# Having problems representing money

• 07-22-2004
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.
• 07-22-2004
erikj
Use fixed before setprecision.

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

This will print 1234.56
• 07-22-2004
Sang-drax
Code:

```#include <iomanip> #include <iostream> using namespace std;   cout << fixed << setprecision(2) << myMoney;```
Hope that helps! :)
• 07-23-2004
Oh wow. Thank you very much. I was putting fixed AFTER setprecision. I guess my book is a little out of date. :D

Actually it didn't work. I get an error that says fixed is undeclared and it says to "first use this function."
• 07-23-2004
erikj
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; }```
• 07-23-2004
Still doesn't work. Does fixed have to be the first thing right after cout? I'll try tinkering around a bit.
• 07-23-2004
prog-bman
what compiler do you have?
• 07-23-2004
I thought that might be the problem. I have Bloodshed Dev-C++ Version 4.
• 07-24-2004
prog-bman
`cout.setf(ios::fixed);`
`cout.unsetf(ios::fixed);`