I have a simple C++ command line program where I am taking an input from the user then displaying it as a price, with a currency symbol, commas separating each set of 3 values, a decimal point, and two decimal places which will display even if they are zero.
Example of what I am trying to achieve:
100 => £100.00
101.1 => £101.10
100000 => £100,000.00
Here is my method so far:
void output_price() {
int price;
cout << "Enter a price" << endl;
cin >> price;
string num_with_commas = to_string(price);
int insert_position = num_with_commas.length() - 3;
while (insert_position > 0) {
num_with_commas.insert(insert_position, ",");
insert_position -= 3;
}
cout << "The Price is: £" << num_with_commas << endl;
}
This is partly working but doesn't show the decimal point/places.
1000 => £1000
If I change price to a float or double it gives me this:
1000 => £10,00.,000,000
I am trying to keep things simple and avoid creating a currency class, however not sure if this is possible in C++.
Any help appreciated.