BigDecimal bd = new BigDecimal("10.123456789"); int scale = bd.scale(); System.out.println("Scale: " + scale);
Scale: 9
BigDecimal bd1 = new BigDecimal("10.123456789"); BigDecimal bd2 = bd1.setScale(4, RoundingMode.HALF_UP); System.out.println("Original value: " + bd1); System.out.println("New value with scale 4: " + bd2);
Original value: 10.123456789 New value with scale 4: 10.1235In the above example, we first create a BigDecimal value with a scale of 9. We then use the setScale() method to set the scale to 4, rounding up to the nearest value. The output shows the original value and the new value with the adjusted scale. The java.math.BigDecimal class is part of the Java standard library.