446,171 Members | 992 Online
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

# Floating point math: rounding up to the next cent

 P: 1 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 Mar 16 '07 #1