2013-05-08 65 views
2

我目前使用固定小数精度

std::cout.precision(5); 

设置我的输出的小数精度。但是,我宁愿让我的 输出总是输出5个小数位(现在它不会显示0)。 我会如何更改我的代码以反映这一点?

回答

0

写一个println函数,它接受一个浮点,并用它来代替COUT的,在内部调用COUT精确

8

您与std::setprecision一起寻找std::fixed

#include <iomanip> 
#include <iostream> 
double f =1.1; 
std::cout << std::fixed; 
std::cout << std::setprecision(5) << f << std::endl; 

标准输出

1.10000 
3

尝试:

std::cout.precision(5); 
std::cout << std::fixed; 
std::cout << a << std::endl; //output a with fixed precision 5 

在这里看到:std::fixed一些例子。

+0

完美!谢谢。 – user2105982 2013-05-08 14:36:45

+0

@ user2105982欢迎您 – taocp 2013-05-08 14:38:00

0

是很方便的事:

std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield); 
std::cout.precision(x);