print double with precision 4 using cout [duplicate]
Asked Answered
C

2

13

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.

Cicisbeo answered 5/12, 2012 at 16:58 Comment(1)
The duplicate doesn't provide a direct answer to the question, the accepted answer does.Corrigible
J
29

Just try:

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

See here.

Jecon answered 5/12, 2012 at 17:0 Comment(0)
T
1
#include <iomanip>
#include <iostream.h>


int main()
{
double a = 0.00;
// print a double, 2 places of precision 
cout << setprecision(4) << a << endl;
}
Tamatave answered 5/12, 2012 at 18:1 Comment(3)
Apart from missing std::s and non existent iostream.h, without std::fixed this simply doesn't work.Pinniped
@Pinniped It works well with visual studioTamatave
@Tamatave This shouldn't work and is not language consistent.Plains

© 2022 - 2024 — McMap. All rights reserved.