Search code examples
javaandroidroundingbigdecimal

BigDecimal.setScale works wrong


Here is my rounding function:

return new BigDecimal("4.780").setScale(2, RoundingMode.HALF_UP).doubleValue();

From my view result should be 4.8, but it returns me 4.78. Can somebody explain why?


Solution

  • As it says in the Javadoc:

    If zero or positive, the scale is the number of digits to the right of the decimal point

    So, if you set it to 2, there will be 2 digits to the right of the decimal point.