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?
5 Answers
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
3It 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; 0 The easiest way to do this, is using cstdio's printf. Actually, i'm surprised that anyone mentioned printf! anyway, you need to include the library, like this...
#include<cstdio>
int main() { double total; cin>>total; printf("%.2f\n", total);
}This will print the value of "total" (that's what %, and then ,total does) with 2 floating points (that's what .2f does). And the \n at the end, is just the end of line, and this works with UVa's judge online compiler options, that is:
g++ -lm -lcrypt -O2 -pipe -DONLINE_JUDGE filename.cppthe code you are trying to run will not run with this compiler options...
3This will be possible with setiosflags(ios::showpoint).
1Using header file stdio.h you can easily do it as usual like c. before using %.2lf(set a specific number after % specifier.) using printf().
It simply printf specific digits after decimal point.
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{ double total=100; printf("%.2lf",total);//this prints 100.00 like as C
}