print double with precision 4 using cout [duplicat

2019-04-04 11:25发布

Possible Duplicate:
Convert a double to fixed decimal point in C++

Suppose , I have double a = 0 and I want to print it as 0.0000 .

I've tried this :

cout.precision(4) ; 
cout<<a<<endl ; 

but it gaves 0 as the output.

2条回答
冷血范
2楼-- · 2019-04-04 11:57
#include <iomanip>
#include <iostream.h>


int main()
{
double a = 0.00;
// print a double, 2 places of precision 
cout << setprecision(4) << a << endl;
}
查看更多
淡お忘
3楼-- · 2019-04-04 12:01

Just try:

#include <iomanip>
...
cout << fixed << setprecision(4);
cout << a << endl;

See here.

查看更多
登录 后发表回答