How to print only 2 places after decimal point

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
WestonGroupAsked:
Who is Participating?
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)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.
0
 
Infinity08Connect With a Mentor Commented:
   double value = 145.3049;
    cout << fixed << setprecision(2) << value << endl;
0
 
Infinity08Commented:
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;
}

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

        http://www.cplusplus.com/reference/iostream/manipulators/setprecision.html
0
 
evilrixSenior Software Engineer (Avast)Commented:
Or, if you're not using streams.
http://www.cplusplus.com/reference/clibrary/cstdio/printf.html
double d = 145.3049;
printf("%.2f", d);

Open in new window

0
 
WestonGroupAuthor 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?

0
 
Infinity08Commented:
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
0
 
WestonGroupAuthor Commented:
when I add fixed it is complied and I get an error saying fixed is  undeclared?

0
 
WestonGroupAuthor 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  

0
 
evilrixSenior Software Engineer (Avast)Commented:
This should work (it does for me): cout << fixed << setprecision(2) << value << endl;
0
 
Infinity08Commented:
>> this complies but prints this 123.4    no zero?

the fixed has to be BEFORE The setprecision, as in my example code.
0
 
WestonGroupAuthor 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
0
 
Infinity08Commented:
Can you show the exact code you're using ?
0
 
WestonGroupAuthor 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;
}
0
 
evilrixSenior Software Engineer (Avast)Commented:
not ios::fixed just fixed!

cout <<" sum = " << fixed <<setprecision(2) << sum <<"\n";
0
 
evilrixSenior Software Engineer (Avast)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);
	}

Open in new window

0
 
WestonGroupAuthor Commented:
when I just put fixed

I get a compiler error saying fixed is undeclared...it will only compile if I put ios::fixed
0
 
evilrixSenior Software Engineer (Avast)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

0
 
evilrixSenior Software Engineer (Avast)Commented:
BTW: you did include <iomanip> right?
0
 
WestonGroupAuthor Commented:
yes I include # include <iomanip>
0
 
evilrixSenior Software Engineer (Avast)Commented:
Then you have problems :)
Meanwhile printf will do what you are trying to do.
0
 
WestonGroupAuthor Commented:
printf compiler error

implicit declaration of function 'int printf(...)'
0
 
evilrixSenior Software Engineer (Avast)Commented:
You included <stdio.h> right?
0
 
WestonGroupAuthor Commented:
yes
0
 
WestonGroupAuthor Commented:
can you provide me a link for where you downloaded your version of dev C++?

I just followed the normal installation instructions so I don't know what I could have done wrong
0
 
evilrixSenior Software Engineer (Avast)Commented:
Visual Studio 2008 Express (free download)
http://www.microsoft.com/express/download/
0
 
Infinity08Commented:
Try changing the deprecated :

#include <iostream.h>

to :

#include <iostream>
0
 
Infinity08Connect With a Mentor Commented:
>> can you provide me a link for where you downloaded your version of dev C++?

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

        http://prdownloads.sourceforge.net/dev-cpp/devcpp-4.9.9.2_setup.exe
0
 
WestonGroupAuthor Commented:
nope didn't work...

0
 
Infinity08Commented:
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.
0
 
WestonGroupAuthor 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)
0
 
evilrixSenior Software Engineer (Avast)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.
0
 
evilrixSenior Software Engineer (Avast)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;
}

Open in new window

0
 
Infinity08Commented:
>> 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.
0
 
evilrixSenior Software Engineer (Avast)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
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> how on earth do I make this thing run in debug?
Never mind I RTM :)
0
 
WestonGroupAuthor Commented:
I uninstalled my version of dev C++ verison 4

and downloaded the beta version and it complied fine..

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?

0
 
evilrixSenior Software Engineer (Avast)Commented:
>> is there a way I can complie and have it auto run?
I just clicked the compile and run button or [F9]
0
 
WestonGroupAuthor 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..

0
 
Infinity08Commented:
>> 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;
        }
0
 
WestonGroupAuthor Commented:
Got it...
Thanks for the help
0
 
Infinity08Commented:
So, the values are showing fine now with fixed and setprecision(2) ?
0
 
WestonGroupAuthor Commented:
Yes everything is working perfectly

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

Thanks again
0
All Courses

From novice to tech pro — start learning today.