I am using the GraffitiCurrencyField to enter and save amounts to a database. I was saving the CurrencyField.value to database and it is a double. But I recently ran into a rounding issue where the double value is rounded the cents up in some values. I do see that the control has some rounding options to choose from. Which one should a choose? I don’t see an option to output the value as currency. I just want the value to be exactly what the user types in. It is set to 2 decimal places.
Also, My license is not current with GraffitiSuite and I don’t have the money to renew right now. But I do really like his plugins.
Thanks
Hi Gary,
I’m adding this information to documentation for the next release:
HalfUpSymmetric (default)
The most common method of rounding numbers is through the Round Half Up technique. This means that any decimal place .5 and above go up.
Here’s a quick example:
- 1.4 rounds down to 1
- 1.5 rounds up to 2
- 1.6 rounds up to 2
.5 is the marker here for the Round Up method, and any number with a decimal of .5 or higher will be rounded up to the next number.
Now, when it comes to negative numbers, up by definition means going somewhere positive. So, -2 is considered higher than -3.
Here’s what we mean:
- -1.4 rounds up to -1
- -1.5 rounds up to -1 (because -1 is “upper” than -2)
- -1.6 rounds down to -2
The opposite of this falls down to the Round Half Down method, which you’ll see below.
HalfUpAsymmetric
Similar to the Symmetric method, except negatives will round toward negative infinitity rather than positive infinity.
HalfDownSymmetric
Round Half Down is the second most common way to round numbers. Instead of .5 being the market for going up, instead, it’s the marker for numbers to go down.
This should help you out:
- 1.4 rounds down to 1
- 1.5 rounds down to 1
- 1.6 rounds up to 2
As you can see, the .5 decimal here was rounded down to 1 instead of the previous method, which rounded it up to 2.
Again, when it comes to negative numbers, the same thing applies. We’re still going in the positive direction, but any number with a .5 decimal will round down to a higher negative value.
Here’s the idea:
- -1.4 rounds up to -1
- -1.5 rounds down to -2 (because -2 is a “lower” than -1, rounding down to a higher negative value)
- -1.6 rounds down to -2
HalfDownAsymmetric
Similar to the Symmetric method, except negatives will round toward positive infinitity rather than negative infinity.
Round-Half-Even “Bankers Rounding”
Rounding half to even is a brilliantly simple solution for such a compelling problem. This is how banks round numbers, and the principle behind it is simple.
Every number without a .5 rounds to the nearest number as normal, but numbers with .5 round to the nearest even number.
Here’s an example:
- 5.5 rounds up to 6 (because 6 is closer to 5.5 than 4)
- 4.5 rounds down to 4 (because 4 is closer to 4.5 than 6)
The opposite of this, of course, is the next method.
Round Half to Odd
With Round Half to Odd, the same rules apply from the previous method, but for odd numbers.
Figure this:
- 5.5 rounds down to 5 (because 5 is closer to 5.5 than 7)
- 4.5 rounds up to 5 (because 5 is closer to 4.5 than 3)
HalfEven
Of course, rounding positive and negative numbers need their own set of rules. Rounding half away from zero is a rounding method that rounds .5 and -.5 away from zero.
Like this:
- 5.5 rounds up to 6 (because 6 is farther than 5 from 0)
- -5.5 rounds down to -6 (because -6 is farther than -5 from 0)
AwayFromZero
Always rounds the result away from 0
Here’s an example:
- 5.5 rounds up to 6
- -5.5 rounds down to -6
TowardZero
Always rounds the result toward 0
Here’s an example:
- 5.5 rounds down to 5 (because 5 is closer than 6 to 0)
- -5.5 rounds up to -5 (because -5 is closer than -6 to 0)
Nearest
Always rounds to the nearest point.
Up
Always rounds up.
Down
Always rounds down.
ToFloor and ToCeiling
Floor and ceiling are common terms in the programming and software engineering industry.
Floor simply means that every number with a decimal is always rounded down.
Take this for example:
- 7.4 rounds down to 7
- 7.5 rounds down to 7
- 7.6 rounds down to 7
- 7.9999 rounds down to 7
Ceiling, on the other hand, is the exact opposite, where every number is always rounded up.
Like this:
- 7.6 rounds up to 8
- 7.5 rounds up to 8
- 7.4 rounds up to 8
- 7.1111 rounds up to 8
Thanks for the great explanation Anthony.
You really shouldn’t be doing that. Doubles lose precision like this all the time. It would be better to store it in a field type specifically for currency or as an integer with the value multiplied by 100 or 1000 depending on the accuracy you need.
Thank you so much for the reply Anthony.