这是我的代码,只是一个基本的测试程序,用于打印带 float 的数字,使用 printf
效果很好(我猜是因为格式已明确传递)但使用 std: :cout
我无法正确处理并且 std::setprecision
似乎没有帮助,你能帮帮我吗?
#include <iostream>
#include <iomanip>
int main ()
{
std::cout << double(1999999900) / 10000 << std::endl;
std::cout << std::setprecision(5) << double(1999999900) / 10000 << std::endl;
printf("%f\n", double(1999999900) / 10000);
return 0;
}
这是输出,我怎样才能从 std::cout
打印也得到 199999.99?
[root@ test00]# g++ -std=c++11 test1.cpp -o test1; ./test1
200000
2e+05
199999.990000
最佳答案
使用
std::cout << std::setprecision(5) << std::fixed << double(1999999900) / 10000 << std::endl;
关于c++ - 精确打印简单的 double 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42780518/