1、枚举 RoundingMode的继承关系
java.math
枚举 RoundingMode
java.lang.Object
java.lang.Enum<RoundingMode>
java.math.RoundingMode
所有已实现的接口:
Serializable, Comparable<RoundingMode>
2、CEILING 的作用
向正无限大方向舍入的舍入模式。
3、FLOOR的作用
向负无限大方向舍入的舍入模式。
4、UP的作用
远离零方向舍入的舍入模式。
5、Down的作用
向零方向舍入的舍入模式。
6、HALF_UP的作用
四舍五入
7、HALF_DOWN的作用
五舍六入
8、HALF_EVEN的作用
向最接近数字方向舍入的舍入模式,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。
|
输入数字 |
使用 HALF_EVEN 舍入模式 |
|
5.5 |
6 |
|
2.5 |
2 |
|
1.6 |
2 |
|
1.1 |
1 |
|
1.0 |
1 |
|
-1.0 |
-1 |
|
-1.1 |
-1 |
|
-1.6 |
-2 |
|
-2.5 |
-2 |
|
-5.5 |
-6 |
9、public static final RoundingMode UNNECESSARY的作用
用于断言请求的操作具有精确结果的舍入模式,因此不需要舍入。如果对生成精确结果的操作指定此舍入模式,则抛出 ArithmeticException。
|
输入数字 |
使用 UNNECESSARY 舍入模式 |
|
5.5 |
抛出 ArithmeticException |
|
2.5 |
抛出 ArithmeticException |
|
1.6 |
抛出 ArithmeticException |
|
1.1 |
抛出 ArithmeticException |
|
1.0 |
1 |
|
-1.0 |
-1 |
|
-1.1 |
抛出 ArithmeticException |
|
-1.6 |
抛出 ArithmeticException |
|
-2.5 |
抛出 ArithmeticException |
|
-5.5 |
抛出 ArithmeticException |
10、RoundingMode的方法
返回与 BigDecimal 中遗留整数舍入模式常量对应的 RoundingMode 对象。
public static RoundingMode valueOf(int rm) { switch(rm) { case BigDecimal.ROUND_UP: return UP; case BigDecimal.ROUND_DOWN: return DOWN; case BigDecimal.ROUND_CEILING: return CEILING; case BigDecimal.ROUND_FLOOR: return FLOOR; case BigDecimal.ROUND_HALF_UP: return HALF_UP; case BigDecimal.ROUND_HALF_DOWN: return HALF_DOWN; case BigDecimal.ROUND_HALF_EVEN: return HALF_EVEN; case BigDecimal.ROUND_UNNECESSARY: return UNNECESSARY; default: throw new IllegalArgumentException("argument out of range"); } }
|
本文详细介绍了java.math.RoundingMode枚举,包括其继承关系和各种枚举值的作用,如CEILING、FLOOR、DOWN、HALF_UP、HALF_DOWN、HALF_EVEN以及UNNECESSARY,并探讨了这些舍入模式在实际运算中的具体行为。
1105

被折叠的 条评论
为什么被折叠?



