BigDecimal有八种RoundingMode,其中最基本的有两种,它们是:
ROUND_UP:Always increments the digit prior to a nonzero discarded fraction.
总是将要丢掉的数字前一位的数字加1.
例如:
Rounding mode UP Examples Input Number | Input rounded to one digit
with UP
rounding
—|—
5.5 | 6
2.5 | 3
1.6 | 2
1.1 | 2
1.0 | 1
-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 Number | Input rounded to one digit
with DOWN
rounding
—|—
5.5 | 5
2.5 | 2
1.6 | 1
1.1 | 1
1.0 | 1
-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 for RoundingMode.DOWN
.如果参数是正数,使用UP规则,如果参数是负数,使用DOWN规则;
Rounding mode CEILING Examples Input Number | Input rounded to one digit
with CEILING
rounding
—|—
5.5 | 6
2.5 | 3
1.6 | 2
1.1 | 2
1.0 | 1
-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 for RoundingMode.UP
.如果参数是正数,使用DOWN规则,如果参数是负数,使用UP规则;
Rounding mode FLOOR Examples Input Number | Input rounded to one digit
with FLOOR
rounding
—|—
5.5 | 5
2.5 | 2
1.6 | 1
1.1 | 1
1.0 | 1
-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 Number | Input rounded to one digit
with HALF_UP
rounding
—|—
5.5 | 6
2.5 | 3
1.6 | 2
1.1 | 1
1.0 | 1
-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 Number | Input rounded to one
digit
with HALF_DOWN
rounding
—|—
5.5 | 5
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 | -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 Number | Input rounded to one
digit
with HALF_EVEN
rounding
—|—
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
ROUND_UNNECESSARY:不需要ROUND
最后来一张总表,
Summary of Rounding Operations Under Different Rounding Modes | Result of
rounding input to one digit with the given rounding mode
—|—
Input Number | UP
| DOWN
| CEILING
| FLOOR
| HALF_UP
| HALF_DOWN
| HALF_EVEN
| UNNECESSARY
5.5 | 6 | 5 | 6 | 5 | 6 | 5 | 6 | throw ArithmeticException
2.5 | 3 | 2 | 3 | 2 | 3 | 2 | 2 | throw ArithmeticException
1.6 | 2 | 1 | 2 | 1 | 2 | 2 | 2 | throw ArithmeticException
1.1 | 2 | 1 | 2 | 1 | 1 | 1 | 1 | throw ArithmeticException
1.0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
-1.0 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1
-1.1 | -2 | -1 | -1 | -2 | -1 | -1 | -1 | throw ArithmeticException
-1.6 | -2 | -1 | -1 | -2 | -2 | -2 | -2 | throw ArithmeticException
-2.5 | -3 | -2 | -2 | -3 | -3 | -2 | -2 | throw ArithmeticException
-5.5 | -6 | -5 | -5 | -6 | -6 | -5 | -6 | throw 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