Search code examples
javabigdecimal

Division with two decimal places not working


I'm trying to divide 193251715/659669075 and get the result of 29.30% (so to round it up to the nearest value with two decimal points)

Here's what I've done:

new BigDecimal(193251715).divide(new BigDecimal(659669075) , 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100))

But I'm getting 29.

What am I doing wrong?


Solution

  • That's because you are multiplying by 100 to show a percentage. You should divide with a scale of 4 instead of 2, then set the scale to 2:

    new BigDecimal(193251715).divide(new BigDecimal(659669075), 4, RoundingMode.HALF_UP)
                             .multiply(new BigDecimal(100))
                             .setScale(2)