我是靠谱客的博主 坚强金毛,最近开发中收集的这篇文章主要介绍Java BigDecimal中的RoundingMode,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

最后

以上就是坚强金毛为你收集整理的Java BigDecimal中的RoundingMode的全部内容,希望文章能够帮你解决Java BigDecimal中的RoundingMode所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部