Why not round it in code?

Leon Torres wrote:
As a follow up, we advise using any of the floor or ceiling rounding modes because GenericValue.getBigDecimal() doesn't return exact numbers. If the database value is 9.9, it will return 9.9000000000000003552713678. Using ROUND_CEILING on this produces 9.91. That's bad!

The whole cause of this thread was behavior of ROUND_HALF_EVEN that goes against what most of us are trained to think when it comes to rounding. For instance, I'm trained to think that 0.825 must be rounded to 0.83. But when using ROUND_HALF_EVEN, I was seeing 0.82 instead. So this made me think that ROUND_HALF_EVEN was doing the wrong thing.

But this turns out that my ingrained thinking is incorrect. If the last digit is 5, which is equidistant from the next higher and lower rounded numbers, then it should be rounded up half the time and down half the time. Hence, the most precise way to round is to use a technique like ROUND_HALF_CEILING.

However since I highly doubt this is common knowledge, especially among customers and users, I think we should move to ROUND_HALF_UP instead, which rounds 0.825 to 0.83 as is commonly accepted. Even if it's imprecise, it's what society expects.

What do you think?

If you want a primer on how BigDecimal works and all these rounding modes and issues, here's an article I wrote,

http://www.opensourcestrategies.com/ofbiz/ofbiz_bigdecimal_cookbook.txt

- Leon



Si Chen wrote:

Wow,

So it looks like everybody does this differently. Well, in that case we'll leave this as is, but make sure you study arithmetic.properties file, understand what the different rounding modes mean, and use the right one.

Si


Reply via email to