BigDecimal舍入模式(Rounding Modes)

1、ROUND_UP(向上舍入)

源码中的解释:

Rounding mode to round away from zero. Always increments the digit prior to a nonzero discarded fraction. Note that this rounding mode never decreases the magnitude of the calculated value.

翻译:

舍入模式从零舍入。总是对丢弃的非零分数前面的数字递增。注意,这种舍入模式不会降低计算值的大小。

// 5
System.out.println(new BigDecimal("4.1").setScale(0, BigDecimal.ROUND_UP));
// -5
System.out.println(new BigDecimal("-4.1").setScale(0, BigDecimal.ROUND_UP));

2、ROUND_DOWN(向下舍入)

源码中的解释:

Rounding mode to round towards zero. Never increments the digit prior to a discarded fraction (i.e., truncates). Note that this rounding mode never increases the magnitude of the calculated value.

翻译:

舍入模式向零舍入。永远不要对丢弃的分数前面的数字加1(即截断)。注意,这种舍入模式不会增加计算值的大小。

// 4
System.out.println(new BigDecimal("4.9").setScale(0, BigDecimal.ROUND_DOWN));
// -4
System.out.println(new BigDecimal("-4.9").setScale(0, BigDecimal.ROUND_DOWN));

3、ROUND_CEILING(以数轴向右舍入)

源码中的解释:

Rounding mode to round towards positive infinity. If the BigDecimal is positive, behaves as for ROUND_UP; if negative, behaves as for ROUND_DOWN. Note that this rounding mode never decreases the calculated value.

翻译:

四舍五入到正无穷大。如果BigDecimal为正数,则与ROUND_UP相同;如果为负数,则表现为ROUND_DOWN。注意,这种舍入模式不会降低计算值。

// 5
System.out.println(new BigDecimal("4.1").setScale(0, BigDecimal.ROUND_CEILING));
// -4
System.out.println(new BigDecimal("-4.9").setScale(0, BigDecimal.ROUND_CEILING));

这个模式我称其为以数轴向右舍入,如图:

BigDecimal向右舍入

4、ROUND_FLOOR(以数轴向左舍入)

向左舍入与向右舍入恰好相反

源码中的解释:

Rounding mode to round towards negative infinity. If the BigDecimal is positive, behave as for ROUND_DOWN; if negative, behave as for ROUND_UP. Note that this rounding mode never increases the calculated value.

翻译:

四舍五入到负无穷。如果BigDecimal为正数,则采用与ROUND_DOWN相同的方式;如果为负数,则表现为ROUND_UP。注意,这种舍入模式不会增加计算值。

// 4
System.out.println(new BigDecimal("4.9").setScale(0, BigDecimal.ROUND_FLOOR));
// -5
System.out.println(new BigDecimal("-4.1").setScale(0, BigDecimal.ROUND_FLOOR));

5、ROUND_HALF_UP(四舍五入)

源码中的解释:

Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up. Behaves as for ROUND_UP if the discarded fraction is ≥ 0.5; otherwise, behaves as for ROUND_DOWN. Note that this is the rounding mode that most of us were taught in grade school.

翻译:

四舍五入模式向“最近的邻居”舍入,除非两个邻居的距离相等,这种情况下取四舍五入。如果丢弃的分数≥0.5,则表现为ROUND_UP;否则,表现为ROUND_DOWN。请注意,这是我们大多数人在小学学到的舍入模式。

// 4
System.out.println(new BigDecimal("4.4").setScale(0, BigDecimal.ROUND_HALF_UP));
// 5
System.out.println(new BigDecimal("4.5").setScale(0, BigDecimal.ROUND_HALF_UP));
// -4
System.out.println(new BigDecimal("-4.4").setScale(0, BigDecimal.ROUND_HALF_UP));
// -5
System.out.println(new BigDecimal("-4.5").setScale(0, BigDecimal.ROUND_HALF_UP));

6、ROUND_HALF_DOWN(五舍六入)

源码中的解释:

Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down. Behaves as for ROUND_UP if the discarded fraction is > 0.5; otherwise, behaves as for ROUND_DOWN.

翻译:

四舍五入模式向“最近的邻居”舍入,除非两个邻居的距离相等,在这种情况下向下舍入。如果丢弃的分数是> 0.5,则表现为ROUND_UP;否则,表现为ROUND_DOWN。

// 4
System.out.println(new BigDecimal("4.5").setScale(0, BigDecimal.ROUND_HALF_DOWN));
// 5
System.out.println(new BigDecimal("4.6").setScale(0, BigDecimal.ROUND_HALF_DOWN));
// -4
System.out.println(new BigDecimal("-4.5").setScale(0, BigDecimal.ROUND_HALF_DOWN));
// -5
System.out.println(new BigDecimal("-4.6").setScale(0, BigDecimal.ROUND_HALF_DOWN));

7、ROUND_HALF_EVEN(靠近偶数舍入)

舍入左边第一位是奇数,则四舍五入;左边是偶数,则五舍六入

源码中的解释:

Rounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for ROUND_HALF_DOWN if it’s even. Note that this is the rounding mode that minimizes cumulative error when applied repeatedly over a sequence of calculations.

翻译:

四舍五入模式向“最近的邻居”舍入,除非两个邻居的距离相等,在这种情况下,向偶数的邻居舍入。如果丢弃的分数左边的数字是奇数,则表现为ROUND_HALF_UP;如果它是偶数,则表现为ROUND_HALF_DOWN。请注意,当在一系列计算中重复应用时,这种舍入模式能最大限度地减少累积误差。

// 4
System.out.println(new BigDecimal("4.4").setScale(0, BigDecimal.ROUND_HALF_EVEN));
// 4
System.out.println(new BigDecimal("4.5").setScale(0, BigDecimal.ROUND_HALF_EVEN));
// 5
System.out.println(new BigDecimal("4.6").setScale(0, BigDecimal.ROUND_HALF_EVEN));
// 5
System.out.println(new BigDecimal("5.4").setScale(0, BigDecimal.ROUND_HALF_EVEN));
// 6
System.out.println(new BigDecimal("5.5").setScale(0, BigDecimal.ROUND_HALF_EVEN));
// 6
System.out.println(new BigDecimal("5.6").setScale(0, BigDecimal.ROUND_HALF_EVEN));
// -4
System.out.println(new BigDecimal("-4.4").setScale(0, BigDecimal.ROUND_HALF_EVEN));
// -4
System.out.println(new BigDecimal("-4.5").setScale(0, BigDecimal.ROUND_HALF_EVEN));
// -5
System.out.println(new BigDecimal("-4.6").setScale(0, BigDecimal.ROUND_HALF_EVEN));
// -5
System.out.println(new BigDecimal("-5.4").setScale(0, BigDecimal.ROUND_HALF_EVEN));
// -6
System.out.println(new BigDecimal("-5.5").setScale(0, BigDecimal.ROUND_HALF_EVEN));
// -6
System.out.println(new BigDecimal("-5.6").setScale(0, BigDecimal.ROUND_HALF_EVEN));

8、ROUND_UNNECESSARY(断言)

源码中的解释:

Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary. If this rounding mode is specified on an operation that yields an inexact result, an ArithmeticException is thrown.

翻译:

舍入模式断言所请求的操作具有精确的结果,因此不需要舍入。如果在产生不精确结果的操作上指定此舍入模式,则会抛出算术异常。

// 断言计算结果包含一位小数则正常输出5.6
System.out.println(new BigDecimal("5.6").setScale(1, BigDecimal.ROUND_UNNECESSARY));

// 断言计算结果不包含小数则报错:java.lang.ArithmeticException: Rounding necessary
System.out.println(new BigDecimal("5.6").setScale(0, BigDecimal.ROUND_UNNECESSARY));
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值