Java BigDecimal中的RoundingMode

本文详细介绍了Java中BigDecimal类的八种RoundingMode及其应用场景,包括ROUND_UP、ROUND_DOWN、ROUND_CEILING等,并通过实例展示了不同模式下数值的处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BigDecimal有八种RoundingMode,其中最基本的有两种,它们是:

ROUND_UP:Always increments the digit prior to a nonzero discarded fraction. 总是将要丢掉的数字前一位的数字加1.

例如:

Rounding mode UP Examples
Input NumberInput rounded to one digit
with UP rounding
5.56
2.53
1.62
1.12
1.01
-1.0-1
-1.1-2
-1.6-2
-2.5-3
-5.5-6

比方说5.5的scale是1,那么需要丢掉的数字的0.5, 将0.5丢掉,然后给5加1,就变成6了;

需要注意的是-5.5,这里的负号是不参加运算的,将它当成5.5,先进行Round up,然后将负号加上;

ROUND_DOWN:Never increments the digit prior to a discarded fraction (i.e., truncates).将要丢掉的数字直接丢掉。

Rounding mode DOWN Examples
Input NumberInput rounded to one digit
with DOWN rounding
5.55
2.52
1.61
1.11
1.01
-1.0-1
-1.1-1
-1.6-1
-2.5-2
-5.5-5

比方说5.5的scale是1,那么需要丢掉的数字的0.5, 直接将0.5丢掉就可以了;

需要注意的是-5.5,这里的负号是不参加运算的,将它当成5.5,先进行Round Down,然后将负号加上;


然后其它五中都是根据Round Down/Up来推出来的;

例如:

ROUND_CEILING:If the result is positive, behaves as for RoundingMode.UP; if negative, behaves as forRoundingMode.DOWN.如果参数是正数,使用UP规则,如果参数是负数,使用DOWN规则;

Rounding mode CEILING Examples
Input NumberInput rounded to one digit
with CEILING rounding
5.56
2.53
1.62
1.12
1.01
-1.0-1
-1.1-1
-1.6-1
-2.5-2
-5.5-5

ROUND_FLOOR:If the result is positive, behave as for RoundingMode.DOWN; if negative, behave as forRoundingMode.UP.如果参数是正数,使用DOWN规则,如果参数是负数,使用UP规则;

Rounding mode FLOOR Examples
Input NumberInput rounded to one digit
with FLOOR rounding
5.55
2.52
1.61
1.11
1.01
-1.0-1
-1.1-2
-1.6-2
-2.5-3
-5.5-6

ROUND_HALF_UP: Behaves as for RoundingMode.UP if the discarded fraction is ≥ 0.5; otherwise, behaves as for RoundingMode.DOWN. 如果要丢掉的数字大于等于0.5,使用UP规则;否则使用DOWN规则;

Rounding mode HALF_UP Examples
Input NumberInput rounded to one digit
with HALF_UP rounding
5.56
2.53
1.62
1.11
1.01
-1.0-1
-1.1-1
-1.6-2
-2.5-3
-5.5-6

ROUND_HALF_DOWN: Behaves as for RoundingMode.UP if the discarded fraction is > 0.5; otherwise, behaves as for RoundingMode.DOWN. 如果要丢掉的数字大于0.5,使用UP规则;否则使用DOWN规则;

Rounding mode HALF_DOWN Examples
Input NumberInput rounded to one digit
with HALF_DOWN rounding
5.55
2.52
1.62
1.11
1.01
-1.0-1
-1.1-1
-1.6-2
-2.5-2
-5.5-5

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 RoundingMode.HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for RoundingMode.HALF_DOWN if it's even. 如果要丢掉的数字不是5的话,需要看这个数字前面的数字的奇偶性,如果是奇数,使用HALF_UP规则;如果是偶数使用HALF_DOWN规则;如果要丢掉的数字是5的话,round到它的偶数邻居上;

Rounding mode HALF_EVEN Examples
Input NumberInput rounded to one digit
with HALF_EVEN rounding
5.56
2.52
1.62
1.11
1.01
-1.0-1
-1.1-1
-1.6-2
-2.5-2
-5.5-6

ROUND_UNNECESSARY:不需要ROUND

最后来一张总表,

Summary of Rounding Operations Under Different Rounding Modes
 Result of rounding input to one digit with the given rounding mode
Input NumberUPDOWNCEILINGFLOORHALF_UPHALF_DOWNHALF_EVENUNNECESSARY
5.56565656throw ArithmeticException
2.53232322throw ArithmeticException
1.62121222throw ArithmeticException
1.12121111throw ArithmeticException
1.011111111
-1.0-1-1-1-1-1-1-1-1
-1.1-2-1-1-2-1-1-1throw ArithmeticException
-1.6-2-1-1-2-2-2-2throw ArithmeticException
-2.5-3-2-2-3-3-2-2throw ArithmeticException
-5.5-6-5-5-6-6-5-6throw ArithmeticException

refs:

http://stackoverflow.com/questions/792237/why-and-how-does-round-half-even-minimize-cumulative-error-when-applied-repeated

jdk document

http://en.wikipedia.org/wiki/Floor_and_ceiling_functions

http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值