How to put a number in dollar format

I need to change a number into dollar format.
e.g. 573 into $5.73

Can anybody help me out here?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <sstream>

using namespace std; // I usually don't do this, but I'm not sure if 'ostringstream' is a member of 'std'.

string convert(int number, int digits);

int main(){
// blah blah blah
return 0;
}

string convert(int number, int digits) {
string converted;
ostringstream converter;
converter << number;
converted = converter.str();
converted = '$' + converted;
converted.insert(digits - 1,".");
return converted;
}


The function changes the number into a string, and then it uses some string functions to add a dollar sign and a decimal point to it. Call it by entering the number wanted to be converted, and then the amount of digits in the number.

cout << convert(573,3);
Last edited on
I edited my last post quite a lot. Sorry about that, but it should be working perfectly now. :)
Topic archived. No new replies allowed.