Thread: best variable to use for money

  1. #1
    Registered User
    Join Date
    Mar 2010
    Toronto<3 can a duh

    best variable to use for money

    float or double?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Neither. Fractional powers of 10 (eg 0.1, 0.01) cannot be represented in any floating point type. So, if you use a floating point type to represent a decimal monetary type (eg dollars made up of 100 cents, pound made up of 100 pence) there will be an error of representation. That error tends to propagate and grow with various operations, such as addition or multiplication, when repeated a significant number of times.

    Better to use some representation that allows precise representation of the various components. I'll leave working out options as an exercise.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM