Making Decimal Places Appear

Jan 22, 2015 at 11:21pm
Hey, Guys! Well, I'm 38 years old and really wanted to pick up programming as just a hobby. I seem to be stuck on a little bit of a problem with making decimal places appear. I'm testing stuff with this program, basically I'm trying to get all the numbers that are being output to have the decimal place out to the hundredth. I've tried using double and float, but just can't seem to get it right. The middle one works, but the first and the second are just not working for me. If someone could please fix this code and tell me why those fixes are needed it'd be greatly appreciated. Thanks!

Last edited on Feb 1, 2015 at 8:17pm
Jan 22, 2015 at 11:56pm
Jan 23, 2015 at 1:19am
Okay, I got it to where the last two have the right decimal places, but the first one is just not showing up as .00. Help, Please!
Last edited on Jan 23, 2015 at 1:59am
Jan 23, 2015 at 1:58am
did you see the example in the link?
Jan 23, 2015 at 2:15am
ok, you can try anyone of the following:

1.cout << showpoint;

2.cout << showpoint << fixed;

3.cout << showpoint << fixed << setprecision(3);


notice #include <iomanip> is needed if you use setprecision()
Jan 23, 2015 at 2:17am
include the lines before your desired cout
Jan 23, 2015 at 2:20am
I actually got it working with the code below, I'm not sure exactly why it worked but it did. I did not have to use #include <iomanip> even though I did use setprecision() why is this?


Last edited on Feb 1, 2015 at 8:17pm
Jan 23, 2015 at 3:29am
notice that, setprecision() and precision() are different.

there is something wrong with 2nd line output of your program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
     float AmountPaid;
     float Commission;
     float Total;
     
     cout << showpoint << fixed << setprecision(2);
     AmountPaid = 600.00 * 21.77;
     cout << "Amount paid for stock: $" << AmountPaid << endl;
     
     Commission = 13062.00 * .02;
     cout << "Amount of commission: $" << Commission << endl;
     
     Total = Commission + AmountPaid;
     cout << "Total amount for purchase: $" << Total << endl;
     
     return 0;
}
Topic archived. No new replies allowed.