Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Rounding decimal

Jeppe Sommer
Ranch Hand
Posts: 270
Hi.

I want to round the decimal value 5.345 up to 5.35. How is that possible using java.math.BigDecimal?

Using the follow code does not round up the last decimal point:

sri ramvaithiyanathan
Ranch Hand
Posts: 109

Check the program.copied the program from roseindia

Jeppe Sommer
Ranch Hand
Posts: 270
Thanks, but the method below also turns the value "5.345f" to "5.34f".

IsnĀ“t it normal to round up the value "0.45" to "0.5" instead of "0.4"?

sri ramvaithiyanathan wrote:
Check the program.copied the program from roseindia

Rob Spoor
Sheriff
Posts: 20495
54
The problem is the floating point values. Check out point 20 of the Beginner's FAQ.

In short, toBePaidTotalAmount is already slightly off directly after declaring. As a result the entire BigDecimal calculation is off. Use the String constructor instead: Now all floats and BigDecimals will print as 5.35.

Campbell Ritchie
Sheriff
Posts: 48424
56
That method with floats doesn't help when you want BigDecimals. Also with floats, the three operations will introduce imprecisions.

Go through BigDecimal and look for methods like divide(), setScale() and round(). Also look at the RoundingMode enum, which you will find a link to in a divide method. You can encapsulate rounding and precision in one object called MathContext, also linked to from BigDecimal. . . . At least I think that code will work

Note the subtle difference between the round and setScale methods. And always remember BigDecimal is immutable; if you want to use the results you have to assign them to a BigDecimal reference.

fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
what I was taught in school (back in the stone age when we only had paper and pencil) was that if the last digit was a '5', you rounded to make it even. so 0.25 would round to 0.2, but 0.35 would round up to 0.4. The idea the teacher gave us was that this way, you are rounding up half the time, and rounding down half the time, so they balance out.

It sounded like crap to me, but that's what we were taught.

Campbell Ritchie
Sheriff
Posts: 48424
56
Depends which country you are in. In most English-speaking countries we were taught "half-up" and think it means "half-away-from-zero", which you can find under RoundingMode as HALF_UP.

There is no absolute or right way to round halves: should you round up, down, towards 0, away from 0, towards an odd number, towards an even number, or even randomly? You will find a bit about IEEE-754R here, but I haven't read enough to know the rationale behind their choosing a particular rounding mode (they actually recommend half-towards-even). I believe the rationale of rounding one way half the time and the other way the rest is the correct explanation, but I don't know whether it is a better explanation than any of the other rounding modes. If there is no absolutely right way to round numbers, then we cannot consider the other ways as absolutely wrong.

And it definitely wasn't stone age when I was at school with paper and pencil (and I am probably older than you ). We thought we were modern.

Jeppe Sommer
Ranch Hand
Posts: 270
Thanks it works ;)

Rob Spoor wrote:The problem is the floating point values. Check out point 20 of the Beginner's FAQ.

In short, toBePaidTotalAmount is already slightly off directly after declaring. As a result the entire BigDecimal calculation is off. Use the String constructor instead: Now all floats and BigDecimals will print as 5.35.