35

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?

rav_kr
  • 434
  • 8
  • 16
DSKVP
  • 663
  • 2
  • 11
  • 19
  • 6
    Really? It gives 100.00 on my system (well, it gives an error for a missing semicolon, but after I fixed that it works as I expect it). Maybe you should post the ACTUAL code you have been testing this with. – Mats Petersson Apr 29 '13 at 13:39
  • are you asking for a fix to your code? or are you asking for any way to print a number with two decimals? you should post the compiler options (or at least, the compiler) you are using to run your code, that would be very usefull to spot the problem ;) – pr00thmatic Dec 27 '13 at 15:30

5 Answers5

83
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

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • 1
    Also, the meaning of `setprecision` depends on how the floating point output is formatted: `fixed`, `scientific` or `floatfmt()` (the default). – James Kanze Apr 29 '13 at 14:20
  • Is this system/implementation dependent? Many are reporting that showpoint should not be necessary... And that's the case also for my system. – Antonio Aug 11 '14 at 12:12
  • @JamesKanze [`defaulfloat`](http://en.cppreference.com/w/cpp/io/manip/fixed) was added to C++11 – nodakai Apr 22 '16 at 15:34
7

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;
James Taylor
  • 6,158
  • 8
  • 48
  • 74
soehtetZ
  • 81
  • 1
  • 1
4

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.cpp

the code you are trying to run will not run with this compiler options...

pr00thmatic
  • 815
  • 9
  • 14
  • This might be useful reading http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c: printf can still be used in C++, but in exceptional cases. – Antonio Aug 11 '14 at 12:17
  • 2
    Lol, how is UVa judge, -lcrypt or any of the other compiler options even remotely relevant to the question? – Phil Williams Oct 14 '16 at 02:51
  • 1
    uhh, 3 years ago answer of myself XD well, back then, when i found this question, i was getting started with competitive programming, so i thought that this "extra info" on my answer could help people that was getting started with competitive programming as well :) – pr00thmatic Oct 14 '16 at 20:05
2

Using 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
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
1

This will be possible with setiosflags(ios::showpoint).

woodleg.as
  • 274
  • 1
  • 4
  • 15
  • `showpoint` should not be necessary here, since he has requested 2 digits after the decimal. his code is correct, and should give the output he says he wants. – James Kanze Apr 29 '13 at 14:19