Java中BigDecimal的8种舍入模式通俗理解

java.math.BigDecimal

不可变的、任意精度的有符号十进制数。BigDecimal 由任意精度的整数非标度值和32位的整数标度(scale)组成。

如果为零或正数,则标度是小数点后的位数。如果为负数,则将该数的非标度值乘以10的负scale次幂。

因此,BigDecimal表示的数值是(unscaledValue × 10-scale)。

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.
 */
public final static int ROUND_UP =           0

舍入远离零的舍入模式。

在丢弃非零部分之前始终增加数字(始终对非零舍弃部分前面的数字加1)。

注意,此舍入模式始终不会减少计算值的大小。

以上为译文;个人通俗理解:就是进1法,即不管后面数字是多大,都进1

2、ROUND_DOWN

接近零的舍入模式。

在丢弃某部分之前始终不增加数字(从不对舍弃部分前面的数字加1,即截短)。

注意,此舍入模式始终不会增加计算值的大小。

/**
 * 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.
 */
public final static int ROUND_DOWN =         1;
以上为译文;个人通俗理解:就是去1法,即不管后面数字是多大,都直接舍去

3、ROUND_CEILING

接近正无穷大的舍入模式。

如果 BigDecimal 为正,则舍入行为与 ROUND_UP 相同;

如果为负,则舍入行为与 ROUND_DOWN 相同。

注意,此舍入模式始终不会减少计算值。

/**
 * Rounding mode to round towards positive infinity.  If the
 * {@code BigDecimal} is positive, behaves as for
 * {@code ROUND_UP}; if negative, behaves as for
 * {@code ROUND_DOWN}.  Note that this rounding mode never
 * decreases the calculated value.
 */
public final static int ROUND_CEILING =      2;
以上为译文;意思很容易理解,up正,down负

4、ROUND_FLOOR

接近负无穷大的舍入模式。

如果 BigDecimal 为正,则舍入行为与 ROUND_DOWN 相同;

如果为负,则舍入行为与 ROUND_UP 相同。

注意,此舍入模式始终不会增加计算值。

/**
 * Rounding mode to round towards negative infinity.  If the
 * {@code BigDecimal} is positive, behave as for
 * {@code ROUND_DOWN}; if negative, behave as for
 * {@code ROUND_UP}.  Note that this rounding mode never
 * increases the calculated value.
 */
public final static int ROUND_FLOOR =        3;
以上为译文;意思很容易理解,up负,down正

5、ROUND_HALF_UP

向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为向上舍入的舍入模式。

如果舍弃部分 >= 0.5,则舍入行为与 ROUND_UP 相同;否则舍入行为与 ROUND_DOWN 相同。

/**
 * Rounding mode to round towards {@literal "nearest neighbor"}
 * unless both neighbors are equidistant, in which case round up.
 * Behaves as for {@code ROUND_UP} if the discarded fraction is
 * ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}.  Note
 * that this is the rounding mode that most of us were taught in
 * grade school.
 */
public final static int ROUND_HALF_UP =      4;
以上为译文;通俗理解 :逢5进1  就是四舍五入

6、ROUND_HALF_DOWN

向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为上舍入的舍入模式。

如果舍弃部分 > 0.5,则舍入行为与 ROUND_UP 相同;否则舍入行为与 ROUND_DOWN 相同。

/**
 * Rounding mode to round towards {@literal "nearest neighbor"}
 * unless both neighbors are equidistant, in which case round
 * down.  Behaves as for {@code ROUND_UP} if the discarded
 * fraction is {@literal >} 0.5; otherwise, behaves as for
 * {@code ROUND_DOWN}.
 */
public final static int ROUND_HALF_DOWN =    5;
以上为译文;通俗理解 :5舍6入

7、ROUND_HALF_EVEN

向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。

如果舍弃部分左边的数字为奇数,则舍入行为与 ROUND_HALF_UP 相同;

如果为偶数,则舍入行为与 ROUND_HALF_DOWN 相同。

注意,在重复进行一系列计算时,此舍入模式可以将累加错误减到最小。

此舍入模式也称为“银行家舍入法”,主要在美国使用。


/**
 * Rounding mode to round towards the {@literal "nearest neighbor"}
 * unless both neighbors are equidistant, in which case, round
 * towards the even neighbor.  Behaves as for
 * {@code ROUND_HALF_UP} if the digit to the left of the
 * discarded fraction is odd; behaves as for
 * {@code 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.
 */
public final static int ROUND_HALF_EVEN =    6;
以上为译文;通俗理解:

四舍六入,五分两种情况。

如果前一位为奇数,则入位,否则舍去。

8、ROUND_UNNECESSARY

断言请求的操作具有精确的结果,因此不需要舍入。

如果对获得精确结果的操作指定此舍入模式,则抛出ArithmeticException。

/**
 * 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
 * {@code ArithmeticException} is thrown.
 */
public final static int ROUND_UNNECESSARY =  7;

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值