Thread: Having problems representing money

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    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. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    Use fixed before setprecision.

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

    This will print 1234.56

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Code:
    #include <iomanip>
    #include <iostream>
    using namespace std;
     
    cout << fixed << setprecision(2) << myMoney;
    Hope that helps!
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    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."
    Last edited by madgolfertom; 07-23-2004 at 08:45 AM. Reason: More to say

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    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. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    Still doesn't work. Does fixed have to be the first thing right after cout? I'll try tinkering around a bit.

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    what compiler do you have?
    Woop?

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    I thought that might be the problem. I have Bloodshed Dev-C++ Version 4.

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Try this instead
    Code:
    cout.setf(ios::fixed);
    [edit]
    oh and if you want to get rid of that setting just do this
    Code:
    cout.unsetf(ios::fixed);
    [/edit]
    Last edited by prog-bman; 07-24-2004 at 01:09 AM.
    Woop?

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    That worked great. Thank you very much, and sorry for blowing a simple question out of proportion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another Coin Counting Program
    By MipZhaP in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2005, 02:02 AM
  2. In it for the money
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-28-2004, 02:17 AM
  3. Integration problems
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 01-14-2003, 02:32 PM
  4. Dirty Money.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 10-01-2002, 05:33 PM
  5. the value of money
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 03-23-2002, 03:36 PM