We help IT Professionals succeed at work.

# How to print only 2 places after decimal point

on
I'm calculating some figures... and I want the program to only print the results with 2 places after decimal point

right now some results are printing out like 145.3049  I just want it to round to 2 places 145.30

thanks
Comment
Watch Question

## View Solutions Only

BRONZE EXPERT
Top Expert 2009
Commented:
double value = 145.3049;
cout << fixed << setprecision(2) << value << endl;
BRONZE EXPERT
Top Expert 2009

Commented:
Or a complete program :

``````#include <iostream>
#include <iomanip>

using namespace std;

int main () {
double value = 145.3049;
cout << fixed << setprecision(2) << value << endl;
return 0;
}
``````
BRONZE EXPERT
Top Expert 2009

Commented:
And here's a reference page for setprecision :

http://www.cplusplus.com/reference/iostream/manipulators/setprecision.html
Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
Or, if you're not using streams.
http://www.cplusplus.com/reference/clibrary/cstdio/printf.html
``````double d = 145.3049;
printf("%.2f", d);
``````

Commented:
What if the user is entering in the data , if I put setprecision to 4 places  12.34  what if the user enters 5 places?

BRONZE EXPERT
Top Expert 2009

Commented:
setprecision together with fixed will always show 2 decimal digits, no matter what input was given.

That's what you want, no ?

So, 123.1234567 will be shown as 123.12
And 123.1 will be shown as 123.10
And 0.1 will be shown as 0.10

Commented:
when I add fixed it is complied and I get an error saying fixed is  undeclared?

Commented:
using namespace std;

cout  << setprecision(2) << ios::fixed << value;

this complies but prints this 123.4    no zero?
but if its a number its 2 places 123.44

Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
This should work (it does for me): cout << fixed << setprecision(2) << value << endl;
BRONZE EXPERT
Top Expert 2009

Commented:
>> this complies but prints this 123.4    no zero?

the fixed has to be BEFORE The setprecision, as in my example code.

Commented:
by using  <<ios::fixed << setprecision(2)

all my output is the same number

I calculate 1 + 2 + 3 and my output becomes 40966

some crazy number... i remove the formatting syntax and my output is normal  6

so not sure whats going on there
BRONZE EXPERT
Top Expert 2009

Commented:
Can you show the exact code you're using ?

Commented:
#include <iostream.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;

int main()
{
double sum;
double a;
double b;
double c;

cout <<"Enter value \n";
cin  >> a;
cout <<"Enter value \n";
cin  >> b;
cout <<"Enter value \n";
cin  >> c;

sum = a + b + c;

cout <<" sum = " << ios::fixed <<setprecision(2) << sum <<"\n";

system("PAUSE");
return 0;
}
Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
not ios::fixed just fixed!

cout <<" sum = " << fixed <<setprecision(2) << sum <<"\n";
Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
The difference: -
``````// std::ios::fixed
// <xiosbase>
template<class _Dummy>
const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::fixed;

//std::fixed
// <ios>
inline ios_base& __CLRCALL_OR_CDECL fixed(ios_base& _Iosbase)
{	// set floatfield to fixed
_Iosbase.setf(ios_base::fixed, ios_base::floatfield);
return (_Iosbase);
}
``````

Commented:
when I just put fixed

I get a compiler error saying fixed is undeclared...it will only compile if I put ios::fixed
Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
You have issues with your compiler and streams (you need to fix this!), I seem to remember this from previous posts, hence I suggested using printf above.
http:#20760125

Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
BTW: you did include <iomanip> right?

Commented:
yes I include # include <iomanip>
Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
Then you have problems :)
Meanwhile printf will do what you are trying to do.

Commented:
printf compiler error

implicit declaration of function 'int printf(...)'
Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
You included <stdio.h> right?

Commented:
yes
Senior Software Engineer (Avast)
SILVER EXPERT
Commented:
Then I'm afraid you have a really messed up development env. There is nothing wrong with the code provided. All examples build and work in my dev env. I think you need to revisit the installation of your compiler and make sure all libraries and headers that should be there are there and they are installed in the correct location. There are standard C/C++ headers that must be there.

Commented:

I just followed the normal installation instructions so I don't know what I could have done wrong
Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
BRONZE EXPERT
Top Expert 2009

Commented:
Try changing the deprecated :

#include <iostream.h>

to :

#include <iostream>
BRONZE EXPERT
Top Expert 2009
Commented:

You had problems with streams before, didn't you ? I would uninstall Dev-C++ completely, and re-install it :

Commented:
nope didn't work...

BRONZE EXPERT
Top Expert 2009

Commented:
Did you try the code from my post with id 20759845 ? Just copy and paste it in a new .cpp file, then compile and run. Don't do anything else - just that.

Commented:
yes

it wouldn't compile

fixed undeclared (first use this function)
(each undeclared identifier is reported only once for each function it appears in)
Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
Are you able to try VS2008? Is there any reason you can't or don't want to use it? I have nothing against devc++, I'm sure it's great (I'm gonna try it out myself) but at least for now it'd get you going.
Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
I just installed (first time ever) Dev C++, compiled the code below and it worked as expected.

Not bad for a free Compiler!. I still prefer VC++ but then I have been using in it in various forms for about 12 years so that's not surprising I guess :)

I see there is also a Pascal version -- ah, Pascal, those were the days ;->
``````#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main()
{
double sum;
double a;
double b;
double c;

cout <<"Enter value \n";
cin  >> a;
cout <<"Enter value \n";
cin  >> b;
cout <<"Enter value \n";
cin  >> c;

sum = a + b + c;

cout <<" sum = " << fixed <<setprecision(2) << sum <<"\n";

system("PAUSE");
return 0;
}
``````
BRONZE EXPERT
Top Expert 2009

Commented:
>> Not bad for a free Compiler!

Oh come on ;) It's gcc basically (the Mingw port actually) ... It's not that surprising ;) I've been happily using Dev-C++ for several years now.
Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
>> It's not that surprising
I was refering to the IDE really -- not the compiler (I know it's mingw).

Anyway I8, how on earth do I make this thing run in debug? It keeps telling me it's not built with debug info and asks if I want to. I say yes, it builds but I still can't debug. BTW: no points for this, I've only installed it cos you said it's good :-p
Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
>> how on earth do I make this thing run in debug?
Never mind I RTM :)

Commented:
I uninstalled my version of dev C++ verison 4

so everything worked....

One last question...when I complie and run with the beta , doesn't run I have to actually go into the stored folder and run the exe...is that normal?

is there a way I can complie and have it auto run?

Senior Software Engineer (Avast)
SILVER EXPERT

Commented:
>> is there a way I can complie and have it auto run?
I just clicked the compile and run button or [F9]

Commented:
I do too...complies but doesn't show me if there were errors and doesn't pull up a dos prompt..

I have to run the exe to run the program..

BRONZE EXPERT
Top Expert 2009

Commented:
>> doesn't run I have to actually go into the stored folder and run the exe...

Well, the command prompt probably briefly pops up and then disappears. You can add this line right before the return 0; at the end of the main :

#include <stdlib.h>

int main(void) {

system("PAUSE");     /* <--- add this line */
return 0;
}

Commented:
Got it...
Thanks for the help
BRONZE EXPERT
Top Expert 2009

Commented:
So, the values are showing fine now with fixed and setprecision(2) ?

Commented:
Yes everything is working perfectly

Upgrading from dev C++ version 4 to the beta verison 9  solved the problem

Thanks again