Floating point math: rounding up to the next cent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eeyore
    New Member
    • Mar 2007
    • 1

    Floating point math: rounding up to the next cent

    I've got a problem that seems to be intractable in a purist sense (there are workarounds when you make certain assumptions about the inputs, but no universal solution that I can find).

    Here's the deal. I've got a floating point variable of any size (float, double, long double, I can make it as big as I like), and the variable contains a monetary value. I need to "round up" this number to the next cent. I'm not "rounding" here, I'm "rounding up". So, 1.35 => 1.35, but 1.35000000623 should become $1.36. Essentially what I need is like a ceil() function, but with a specifiable number of decimal places.

    So, one would think, I could just do this:

    value = ceil(input * 100)/100;

    And value would receive the correct result. The problem is that this function has two steps, and the first step messes up the result. For example, with floating point math, .07 * 100 != 7.0 -- it is slightly more than 7.0, and this causes the ceil() function to add a cent when it shouldn't.

    Is there a correct way to do this?

    Thanks!

    --chris
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by eeyore
    I've got a problem that seems to be intractable in a purist sense (there are workarounds when you make certain assumptions about the inputs, but no universal solution that I can find).

    Here's the deal. I've got a floating point variable of any size (float, double, long double, I can make it as big as I like), and the variable contains a monetary value. I need to "round up" this number to the next cent. I'm not "rounding" here, I'm "rounding up". So, 1.35 => 1.35, but 1.35000000623 should become $1.36. Essentially what I need is like a ceil() function, but with a specifiable number of decimal places.

    So, one would think, I could just do this:

    value = ceil(input * 100)/100;

    And value would receive the correct result. The problem is that this function has two steps, and the first step messes up the result. For example, with floating point math, .07 * 100 != 7.0 -- it is slightly more than 7.0, and this causes the ceil() function to add a cent when it shouldn't.

    Is there a correct way to do this?

    Thanks!

    --chris
    If you're just looking down to cents (and nothing further like tenths or hundredths of a cent), I would recommend writing the rounding function yourself, in which case floating-point shouldn't be a problem (even if you hit .005 exactly, that will equal the variable that was input, so the proper action will be taken). If you're looking for more precision than that... I have no idea.

    Comment

    • DeMan
      Top Contributor
      • Nov 2006
      • 1799

      #3
      I'm not sure that this necessarily answers the question either but:

      The problem is caused by the fact that computers are binary. so Non-integer values are reresented such that there is a bit for 1/2 a bit for 1/4 a bit for 1/8 etc (1/(2^n)). This means that floats quite often can't represent an accurate value.
      I would suggest trying to do any calculations in cents and only convert to dollars to print (that is print using cents/100 and force two decimal places....)

      Comment

      Working...