Do I get wrong output with BigDecimal ROUND_HALF_EVEN rounding?

247 Views Asked by At

I have a problem with rounding of BigDecimal numbers using ROUND_HALF_EVEN as described here.

I want to round to 2 decimal places:

BigDecimal number1 = new BigDecimal("23.867995");
BigDecimal number2 = new BigDecimal("23.868");
Log.d("tag", number.setScale(2, BigDecimal.ROUND_HALF_EVEN) + ", " + );

Ouptut:

23.87

But the digit 6 is even, so it should be 23.86 or not?

1

There are 1 best solutions below

1
On

It's round half even, not round everything even. When a number is halfway between two options for what it could round to, it goes toward the even digit. If it's closer to one option than the other, it picks the closer option.