← Back to topics
Topic

map[used tasks] - precision question

v
vasja
I solved this problem using cout.precision(4); because when i used cout.precision(2); it didnt output the decimals. Before this i thought that the parameter in precision function is the number of decimal places it will show.

Can anyone explain how this really works?
A
Al3kSaNdaR
I don't know how that work but use printf("0.2f",sol). It's faster. ;) ( I think... )
v
vasja
:D i know about that , but what i'm asking is why cout.precision(4) is 2 decimal places precision
T
Tavo92
Try googling before asking: http://www.cplusplus.com/reference/iostream/ios_base/precision.html
u
uros94
I guess you need to include
cout << fixed

too :)

Here's example program:

#include <iostream>
#include <iomanip>

int main(void) {
float f;
cin >> f;
cout << fixed << setprecision(2) << f << endl;
return 0;
}


That should do it :)
u
uros94
Ah, I forgot to put using namespace std; :)
f
frank44
If you don't use "fixed" it wont print out trailing zeros.

So if you are trying to:
cout << setprecision(4) << sqrt(.16) ;

this would print: .4

However if you do this:
cout << fixed << setprecision(4) << sqrt(.16);

the result would be: .4000