Quick and stupid C++ question.

BD2003

Lifer
Oct 9, 1999
16,815
1
81
When I display a floating point variable in a string, how do I force it to display a specifc number of decimal points? I'm working with currency, so I need two decimal points. Ex:

cout<< "Adults " << numAdult << " at " << adMealCost << setw(12) << totalAdultCost << "\n";

How do I make the Meal costs appear as 1.50 instead of 1.5?
 

MacBaine

Banned
Aug 23, 2001
9,999
0
0
add this tag..

setiosflags(ios::float || ios::fixed);
setprecision(2);

I am pretty sure that's right.. I may have part of the setios thing wrong, but see how that works


EDIT: fixed it
 

BD2003

Lifer
Oct 9, 1999
16,815
1
81
Well, heres the deal. I'm trying to help out a friend with her homework, and its a first year course, so I cant use any of that fancy schmancy stuff.

All I wanna know is how to make everything in that one line I showed display with 2 decimal places, as simply as possible.
 

DougK62

Diamond Member
Mar 28, 2001
8,035
6
81
Use it in your cout stream just like setw().

Or you can always use the tried and true method of:

floor(100 * (number+.005)) * .01

 

MacBaine

Banned
Aug 23, 2001
9,999
0
0
Originally posted by: BD2003
Well, heres the deal. I'm trying to help out a friend with her homework, and its a first year course, so I cant use any of that fancy schmancy stuff.

All I wanna know is how to make everything in that one line I showed display with 2 decimal places, as simply as possible.

What I just told you is first year material... because I am in my first year and we learned that within the first couple months. Then again I am in an AP class...

You can just do

cout << setprecision(2) << setw(x) << number;

that should work. otherwise, just try adding

setprecision(2);

at the beginning of the program, in the declarations area of int main.
 

DougK62

Diamond Member
Mar 28, 2001
8,035
6
81
cout << "Adults " << numAdult << " at " << adMealCost << setw(12) << floor(100 * (totalAdultCost+.005)) * .01 << "\n";

EDIT: This is definitely first year material. Don't forget your math.h header.
 

BD2003

Lifer
Oct 9, 1999
16,815
1
81
First year for you maybe, but this is a community college, and I only have programmed in C, not C++. My first instinct was to use printf, but alas I cannot.

I cant figure out how to get setprecision to work. Sigh.
 

DougK62

Diamond Member
Mar 28, 2001
8,035
6
81
Originally posted by: BD2003
First year for you maybe, but this is a community college, and I only have programmed in C, not C++. My first instinct was to use printf, but alas I cannot.

I cant figure out how to get setprecision to work. Sigh.

How about breaking up the cout and using an IF statement?

cout<< "Adults " << numAdult << " at " << adMealCost;

if (adMealCost %1.0 == 0)
cout << ".00";
else if(adMealCost %0.1 == 0)
cout << "0";
cout << endl;

Then you could do thesame thing for your totalAdultCost. It's not pretty, but it does the job
 

neit

Senior member
Dec 6, 2001
353
0
0
here's what i think you want to do:

#include <iomanip>
#include <iostream>
..
..
..

cout << setiosflags(ios::fixed)
<< setprecision(2)
<< "Adults: "
<< numAdult << endl;
...
...
...

Include iomanip header, and just put in setiosflags before you need to use the numbers in your cout statement, and set precision does what it says.
 
Last edited:

BD2003

Lifer
Oct 9, 1999
16,815
1
81
Got it working. Needed this line:

<FONT face=Tahoma color=#000000>cout << setiosflags(ios::fixed)

Hopefully that was the right way to do it. Its pretty hard to do someone's homework when you don't know what you can or cant do....</FONT>