print double with precision 4 using cout

URL87 picture URL87 · Dec 5, 2012 · Viewed 40.6k times · Source

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.

Answer

SeMeKh picture SeMeKh · Dec 5, 2012

Just try:

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

See here.