BidDecimal decimal places formatting

Correct spelling:
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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326371697&siteId=291194637