By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 992 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
2 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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.
Mar 16 '07 #2

DeMan
100+
P: 1,806
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....)
Mar 17 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.