best variable to use for money

This is a discussion on best variable to use for money within the C++ Programming forums, part of the General Programming Boards category; float or double?...

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

    best variable to use for money

    float or double?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,259
    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%.

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, 12:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  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, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21