Making Decimal Places Appear

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
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
did you see the example in the link?
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()
include the lines before your desired cout
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
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.