用oststream绕开浮子
-
04-10-2019 - |
题
我使用OSTRINGSTREAM从Float转换为C ++字符串有一个问题。这是我的台词:
void doSomething(float t)
{
ostringstream stream;
stream << t;
cout << stream.str();
}
当t具有-0.89999值时,它的圆形为-0.9,但是当其值为0.0999或少于1.754e -7时,它就不会打印出来。解决方案是什么。
解决方案
您需要使用使用的精度 precision
例如
stream.precision(3);
stream<<fixed; // for fixed point notation
//cout.precision(3); // display only
stream << t;
cout<<stream.str();
其他提示
如果您想要显示特定数量的重要数字,请尝试使用SetPrecision(n),其中n是您想要的重要数字数量。
#include <iomanip>
void doSomething(float t)
{
ostringstream stream;
stream << std::setprecision(4) << t;
cout << stream.str();
}
不隶属于 StackOverflow