bigdecimal.Round_UP
时间: 2024-05-07 19:15:47 浏览: 87
The `BigDecimal` class in Java provides a method `setScale(int newScale, RoundingMode roundingMode)` that can be used to round a `BigDecimal` value to a specified number of decimal places with a specified rounding mode.
To round up a `BigDecimal` value, you can use the `RoundingMode.UP` constant as the `roundingMode` parameter. This rounding mode rounds towards positive infinity, so any value with a non-zero fractional part will be rounded up to the next highest value.
Here's an example:
```java
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Example {
public static void main(String[] args) {
BigDecimal value = new BigDecimal("3.14159");
BigDecimal roundedValue = value.setScale(2, RoundingMode.UP);
System.out.println(roundedValue); // output: 3.15
}
}
```
In this example, the `value` `BigDecimal` is rounded up to 2 decimal places using the `RoundingMode.UP` constant as the rounding mode. The resulting `roundedValue` `BigDecimal` is then printed to the console.
阅读全文