Show two digits after decimal point in c++

28

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?

This question is tagged with c++ precision

~ Asked on 2013-04-29 13:37:01

The Best Answer is


73

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

~ Answered on 2013-04-29 13:41:33


5

It is possible to print a 15 decimal number in C++ using the following:

#include <iomanip>
#include <iostream>

cout << fixed << setprecision(15) << " The Real_Pi is: " << real_pi << endl;
cout << fixed << setprecision(15) << " My Result_Pi is: " << my_pi << endl;
cout << fixed << setprecision(15) << " Processing error is: " << Error_of_Computing << endl;
cout << fixed << setprecision(15) << " Processing time is: " << End_Time-Start_Time << endl;
_getch();

return 0;

~ Answered on 2015-03-20 14:02:59


Most Viewed Questions: