Show two digits after decimal point in c++

DSKVP picture DSKVP · Apr 29, 2013 · Viewed 301.3k times · Source

Similar topic is already discussed in the forum. But I have some different problem in following code:

double total;
cin >> total;
cout << fixed << setprecision(2) << total;

If I give input as 100.00 then program prints just 100 but not 100.00

How can I print 100.00?

Answer

Armen Tsirunyan picture Armen Tsirunyan · Apr 29, 2013
cout << fixed << setprecision(2) << total;

setprecision specifies the minimum precision. So

cout << setprecision (2) << 1.2; 

will print 1.2

fixed says that there will be a fixed number of decimal digits after the decimal point

cout << setprecision (2) << fixed << 1.2;

will print 1.20