Specific Decimal Placement

closed account (N6fwAqkS)
Hi there, I've been toying around with this small program, and have only run into one problem. In the case that the total price has a 0 as its last decimal place, it does not display that 0, only the number prior (e.g. If the total price was 125.50, it would display 125.5) If anyone knows how to fix that, it'd be great.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()

{
    string my;
    float price, quantity;
    
    cout << "Enter price: $";
    getline (cin, my);
    stringstream(my) >> price;
    cout << "Enter quantity: ";
    getline (cin, my);
    stringstream (my) >> quantity;
    cout << "Total Price: $" << price*quantity  << endl;
    return 0;
    
}
Last edited on
try this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
   double num = 3;
   cout << setprecision(2) << fixed << showpoint;
   cout << num << endl;
   num = 0;
   cout << num << endl;
   

    system("PAUSE");
    return 0;
}


you can also use the fill() function
closed account (N6fwAqkS)
Thanks for the help ats15, I was able to use a fixed set precision of 2 to fix this problem. Unfortunately OUIJ, I wasn't able to solve my problem with the code, thanks for the help though!
Topic archived. No new replies allowed.