将数字从长整型转换为字符串 [英] converting number from long double to string

查看:91
本文介绍了将数字从长整型转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数字从long double转换为string.我将输入一个长整型数字,并将返回的相同数字以字符串形式返回,这是我的代码,由于数组,我得到了一个错误.我什至不知道我的代码是对还是错

I want to convert the number from long double to string . I will enter a number in long double and will return the same number back in string this is my code and I''m getting an error due to the array . I even don''t know if my code is right or wrong

string lditoms(long double money[8])
{
    string amnt;
    for(int i=0;i<8;i++)
    {
      amnt=(amnt[i]*10)+money[i]-0;
    }
    return amnt;

}
int main()
{
    long double money[8];
    cout<<"enter value not more than 8 digits";
    for(int i=0;i<8;i++)
    {
    cin>>money[i];

    }
    cout<<lditoms(money);

}


谢谢
:)


thank you
:)

推荐答案

您在这里遇到了一些问题.

1.您说我想将数字从长整型转换为字符串."可以,但是函数lditoms将ld的数组作为输入,但是只有一个输出.我猜您想为每个ld调用一次lditoms,从而为每个ld调用1个字符串.

2.假设(1)是正确的,函数lditoms的输入参数应写为long double money;不需要数组.

3.在lditoms中,不需要for循环,因为现在您只有一个输入参数,而不是数组.

4.在main()中,将lditoms插入for循环中;输入一个ld,调用lditoms.并且无需将money做成数组.

我完全不确定这是您真正想要的,但是它符合您所说的. :)
You have several problems going on here.

1. You say "I want to convert the number from long double to string." OK, but the function lditoms takes an array of ld''s as input, but only has one output. I''m guessing you want to call lditoms once for each ld, and thus get 1 string for each ld.

2. Assuming (1) is correct, the input parameter of function lditoms should be written as long double money; there''s no need for an array.

3. Inside lditoms, there''s no need for a for loop, since now you only have one input parameter, not an array.

4. In main(), stick lditoms in the for loop; input an ld, call lditoms. And there''s no need to make money an array.

I''m not at all sure this is what you really want, but it conforms to what you said you wanted. :)




我将输入一个长双精度数字并返回字符串中的相同数字不足以指定您的功能,因为您将不得不舍入到一些个数字,或者对很小或非常大的数字使用科学计数法.

因此,您的问题一点都不琐碎.

您应该使用标准C ++库的内置功能,例如,此函数将floatdoublelong double转换为std::string.输出舍入为decimal_digits位数字:
Hi,

I will enter a number in long double and will return the same number back in string is not enough to specify your function, as you will have to round to some number of digits, or use scientific notation for very small or very large numbers.

So your question is not at all trivial.

You should use the built-in facilities of the Standard C++ Library, for instance this function converts a float, or double, or long double to a std::string. The output is rounded to decimal_digits digits:
#include <iomanip>
#include <sstream>
template <class Val>
std::string ToString(Val val, size_t decimal_digits = 2)
{
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(decimal_digits) << val << std::ends;
    return oss.str();
}


您可以使用以下方法进行测试:


You may test it with:

#include <iostream>
int main()
{
    std::cout << ToString(123.456) << std::endl; // "123.46"
    std::cout << ToString(12.3456) << std::endl; // "12.35"
    std::cout << ToString(12.3456e10) << std::endl; // "123456000000.00"
    std::cout << ToString(0.0123456) << std::endl; // "0.01"
}


欢呼,
AR


cheer,
AR


首先,我必须问...为什么在世界上为什么要使用long double存储0到9之间的值? int就足够了!

其次,您必须了解amnt=(amnt[i]*10)+money[i]在代码中的实际作用.

字符串实际上只是一个char's数组,因此您必须从那里开始.了解整数的char表示形式使用ascii表.因此,如果money [0] = 4,则将其转换为char将得到EOT的char.但是,将money [0] + 48转换为char可以得到4的char,因为0的ascii值为48.

要实现的另一件事是,字符串加上刚连接的字符串是.因此,如果amnt ="45"并添加"8",它将变为"458",而不是"57".

所以,按照您做的方式,您真的想要

amnt = amnt +(money [i] + 48)

但是,如果此人只想输入45作为数字怎么办?使用您的解决方案,他们将必须输入00000045才能运行程序.

Hans是正确的,因为您应该将单个整数作为输入.

然后,您可以将代码修改为如下所示:

First of all, I have to ask...why in the world are you using a long double to store values between 0 and 9? int would have sufficed!

Secondly, you have to understand what amnt=(amnt[i]*10)+money[i] is actually doing in the code.

A string is really just an array of char''s, so you have to start there. Understand that a char representation of an integer uses an ascii table. So, if money[0] = 4, then a converting that to a char will give you a char of an EOT. But, money[0] + 48 converted to a char gives a char of 4 because the ascii value for 0 is 48.

The other thing to realize is that a string plus a string just concatenates is. So, if amnt = "45" and you add "8", it will become "458", not "57".

So, the way that you''ve done it, you would really want

amnt = amnt + (money[i] + 48)

But, what if the person only wants to enter 45 as their number? With your solution, they would have to enter 00000045 to get the program to run.

Hans is correct in that you should take a single integer as an input.

Then, you could modify your code to look like:

string convertInt(int number)
{
    if (number == 0)
        return "0";
    string temp="";
    string returnvalue="";
    while (number>0)
    {
        temp+=number%10+48;
        number/=10;
    }
    for (int i=0;i<temp.length();i++)
        returnvalue+=temp[temp.length()-i-1];
    return returnvalue;
}



或者,您也可以只使用一个字符串流(尽管如果这是与您的教授进行课堂检查,是否可以使用尚未学习的对象).



or you could just use a stringstream (though if this is for a class check with your professor if you can use objects you haven''t learned yet).


这篇关于将数字从长整型转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆