473,466 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Floating point math: rounding up to the next cent

1 New Member
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
2 3111
sicarie
4,677 Recognized Expert Moderator Specialist
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
1,806 Top Contributor
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

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

Similar topics

13
by: tings | last post by:
An article states: "In floating point maths, where if you divide by a sufficiently large number sufficiently often, you will always be able to reach a value too small to distinguish from zero,...
2
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
9
by: Klaus Bonadt | last post by:
I have found strange behaviour in casting floating point values in C++ 6.0 to int: If I enter in the watch window while debugging in version 6.0 the following term: (1.4 - 1.0) * 10.0 the...
9
by: David Veeneman | last post by:
I'm working on a project that uses floating-point values (doubles), and I'm being driven crazy by something pretty basic. I understand that it's in the nature of floating-point calculations to...
9
by: Steven D'Aprano | last post by:
I'm looking for some way to get the next floating point number after any particular float. (Any mathematicians out there: I am aware that there is no "next real number". But floats are not real...
4
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.