# Thread: Having problems representing money

1. ## Having problems representing money

This is a very amatuerish question, but no matter what I try, there is no flexibility with setprecision when it comes to me wanting just two places beyond the decimal. When I use setprecision(2) and enter a value as 3.75, for instance, I get 3.8. So setprecision goes by the entire numbers' digits, right? And I also have to represent another float, starting at 1000.00, as ongoing funds. If I set the precision at 6, and the funds are less than 1000, it rounds to three digits behind the decimal and I get the same problem I had with the smaller number when the funds are greater than 9999.99. Is there a more flexible standard way to represent money, or will I have to write a rounding script? And if so, I'm new to programming, and am a little lost on how to go about writing one. I'd like to know if there's a standard way to solve this problem, though.

2. Use fixed before setprecision.

cout <<fixed<<setprecision(2) << 1234.56789<<endl

This will print 1234.56

3. Code:
#include <iomanip>
#include <iostream>
using namespace std;

cout << fixed << setprecision(2) << myMoney;
Hope that helps!

4. Oh wow. Thank you very much. I was putting fixed AFTER setprecision. I guess my book is a little out of date.

Actually it didn't work. I get an error that says fixed is undeclared and it says to "first use this function."

5. Strange, does this work?

Code:
#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char* argv[])
{
cout << fixed << setprecision(2) << 1234.5678;

return 0;
}

6. Still doesn't work. Does fixed have to be the first thing right after cout? I'll try tinkering around a bit.

7. what compiler do you have?

8. I thought that might be the problem. I have Bloodshed Dev-C++ Version 4.