BigDecimal decimal = new BigDecimal(12.12345); decimal= decimal.setScale(2, RoundingMode.HALF_DOWN); System.out.println(decimal);//Take two decimals and round up
It won't work if you write:
BigDecimal decimal = new BigDecimal(12.12345); decimal.setScale(2, RoundingMode.HALF_DOWN); System.out.println(decimal);
Why?
Because the setScale method will return a new BigDecimal object, and this new object is what we need.
You can output the hashcode() of the two BigDecimal objects and find that they are indeed different.