tags:

views:

524

answers:

3

First of all see the following problem:

SetRoundMode(rmUp) and rounding “round” values like 10, results in 10,0001.

I need to round currency values up, so 0.8205 becomes 0.83, but the SimpleRoundTo behavior displayed above is giving me some headaches.

How can I round currency values up in a safe way?

+5  A: 

You can use the Ceil function:

newvalue := Ceil(oldvalue * 100) / 100;
Gamecat
+2  A: 

Note that rounding 0.8205 up to 0.83, and also rounding 0.8305 up to 0.84, will result in an upward bias on average in your rounding. The default rounding mode is bankers rounding, which rounds towards even numbers to avoid a directional bias.

This is particularly important if there is a double-entry nature to your calculations. Rounding with a directional bias can result in a mismatch on either side.

Using SetRoundMode changes the FPU control word. Be aware that this FPU mode rounding is applied to floating-point operations in situations that might not be obvious when thinking in terms of the Currency type, which is a fixed-point type (scaled 64-bit integer). A small imprecision in intermediate floating-point calculations, such as 82.000000000000001, will end up rounding up even when the value as Currency is anticipated to be 82.00. Changing the thread-global rounding mode is only to be done with caution.

Barry Kelly
+2  A: 

You're doing it wrong.

Don't use floats to represent important types like time and money!

Use integers that represent the highest precision you need. For example use an integer that represents 1000th of a cent. Then you can pass around 82050 around and when you finally need to display it as a string then and only then do you do the rounding using integer calculations.

To actually answer your question, $0.8205 should not be rounded up. $0.825 should be.

Pyrolistical
This is what I've seen done in Delphi too, lacking a fixed-decimal type.
flatline
If you look at the linked question - by the same poster - you'll see that he *is* using Currency, which is *not* a float (Delphi does have a fixed-decimal type, flatline).
Barry Kelly
Well, it seems the OP *wants* to round up 0.8205 to 0.83, so who are we to tell him he shouldn't? Even it's not standard rounding.
stevenvh