The Big Decimal class is hard to use unless you are involved in work that deals with the precision of numbers. So, when I played with it for the first time in a long time, I was so addicted to it that I wasted my time.
BigDecimal setScale(int newScale) setScale(int newScale, int roundingMode) setScale(int newScale, RoundingMode roundingMode)
Display the processing result with one decimal place
public static void main (String[] args) {
BigDecimal deci = new BigDecimal("1.2345");
System.out.println("deci 1:" + deci);
deci.setScale(1, BigDecimal.ROUND_DOWN);
System.out.println("deci 2:" + deci);
}
deci 1:1.2345 deci 2:1.2345
??? By the way, with this execution result, those who are polite will be JavaDoc first, and those who are familiar with it will be "Ah ...", but I wasted this and that, It took about 20 minutes to resolve it. .. .. (> <). why? When. .. ..
public static void main (String[] args) {
BigDecimal deci = new BigDecimal("1.2345");
System.out.println("deci 1 :" + deci);
deci.setScale(1, BigDecimal.ROUND_DOWN);
System.out.println("deci 2 :" + deci);
BigDecimal deciDecci = deci.setScale(1, BigDecimal.ROUND_DOWN);
System.out.println("deciDecci :" + deciDecci);
}
deci 1 :1.2345 deci 2 :1.2345 deciDecci :1.2
After all, setScale was a method that returned a return value rather than changing the contents of the instance. .. .. I thought I wanted it to be named like toString (), but I should have carefully looked at JavaDoc once. There are various sets and setXXX in the Calendar class and so on.
Recommended Posts