java bigdecimal.setscale
时间: 2024-03-24 07:22:37 浏览: 73
java BigDecimal操作
BigDecimal.setScale method is used to set the scale (number of digits after the decimal point) of a BigDecimal object. The method takes two parameters: the desired scale and the rounding mode.
Syntax:
public BigDecimal setScale(int newScale, RoundingMode roundingMode)
Parameters:
newScale: The new scale to be set.
roundingMode: The rounding mode to be used when rounding.
Example:
BigDecimal bd = new BigDecimal("123.456789");
BigDecimal result = bd.setScale(2, RoundingMode.HALF_UP);
System.out.println(result); // Output: 123.46
In this example, we create a BigDecimal object with the value "123.456789". We then use the setScale method to set the scale to 2 and the rounding mode to HALF_UP. The resulting BigDecimal object is then printed, which outputs "123.46".
阅读全文