我对什么max_digits10
代表感到困惑.根据其文档,对于所有整数类型,它为0.浮点类型的公式max_digits10
看起来类似于int
's digits10
'.
如何将浮点转换为字符串,并在浮点为圆数时添加2位小数.目前我正在使用以下内容,但对于整数,我希望添加.00.我需要为此编写自己的例程吗?
float floatingNumber = 1.00;
string string;
ostringstream stream;
stream << floatingNumber;
string += stream.str(); // result is 1 not 1.00
Run Code Online (Sandbox Code Playgroud) 我对字符串流有一个小问题。当我用它将字符串转换为双精度时,它会失去精度。
const std::string str = "44.23331002";
double x;
stringstream ss;
ss << str;
ss >> x;
cout << str << " = " << x << endl;
Run Code Online (Sandbox Code Playgroud)
输出为: 44.23331002 = 44.2333
为什么是这样?它是否转换为浮点数并且数字精度有限?