main.java
来自「java的经典例子」· Java 代码 · 共 37 行
JAVA
37 行
import java.math.*;
class Main {
static void round(String num, int scale, int roundingMode) {
System.out.println(new BigDecimal(num).setScale(scale, roundingMode));
}
public static void main(String[] args) {
// Increasing the scale.
BigDecimal a = new BigDecimal("1.23");
System.out.println(a.setScale(3)); // 1.230
// Decreasing the scale.
round(".5", 0, BigDecimal.ROUND_CEILING); // 1
round(".5", 0, BigDecimal.ROUND_DOWN); // 0
round(".5", 0, BigDecimal.ROUND_FLOOR); // 0
round(".5", 0, BigDecimal.ROUND_HALF_DOWN); // 0
round(".5", 0, BigDecimal.ROUND_HALF_EVEN); // 0
round(".5", 0, BigDecimal.ROUND_HALF_UP); // 1
round(".4", 0, BigDecimal.ROUND_HALF_UP); // 0
round(".4", 0, BigDecimal.ROUND_UP); // 1
//round(".5", 0, BigDecimal.ROUND_UNNECESSARY); // ArithmeticException
round("1.0", 0, BigDecimal.ROUND_UNNECESSARY); // 1
round("-.5", 0, BigDecimal.ROUND_CEILING); // 0
round("-.5", 0, BigDecimal.ROUND_DOWN); // 0
round("-.5", 0, BigDecimal.ROUND_FLOOR); // -1
round("-.5", 0, BigDecimal.ROUND_HALF_DOWN); // 0
round("-.5", 0, BigDecimal.ROUND_HALF_EVEN); // 0
round("-.5", 0, BigDecimal.ROUND_HALF_UP); // -1
round("-.4", 0, BigDecimal.ROUND_HALF_UP); // 0
round("-.4", 0, BigDecimal.ROUND_UP); // -1
//round("-.5", 0, BigDecimal.ROUND_UNNECESSARY); // ArithmeticException
round("-1.0", 0, BigDecimal.ROUND_UNNECESSARY); // -1
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?