【Java】BigDecimal四舍五入模式详解

一、源代码总览

// Rounding Modes

/**
 * 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;

/**
 * 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;

/**
 * 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;

/**
 * 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;

/**
 * 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;

/**
 * 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;

/**
 * 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;

/**
 * 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;

二、详解

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.234 => 1.24,1.230 => 1.24。

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.
 */
public final static int ROUND_DOWN =         1;
  • 接近零的舍入模式。
  • 对应【1. ROUND_UP 模式】,直接舍弃保留位数后面的,最后一位不+1
    • 如保留两位:1.234 => 1.23,1.230 => 1.23。

3. ROUND_CEILING 模式

/**
 * 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;
  • 接近正无穷大的舍入模式。
  • 如果 BigDecimal 为正,则舍入行为与【1. ROUND_UP 模式】相同。
    • 如保留两位:1.234 => 1.24,1.230 => 1.24。
  • 如果 BigDecimal 为负,则舍入行为与【2. ROUND_DOWN 模式】相同。
    • 如保留两位:-1.234 => -1.23,-1.230 => -1.23。

4. ROUND_FLOOR 模式

/**
 * 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;
  • 接近负无穷大的舍入模式。
  • 如果 BigDecimal 为正,则舍入行为与【2. ROUND_DOWN 模式】相同。
    • 如保留两位:1.234 => 1.23,1.230 =>1.23。
  • 如果 BigDecimal 为负,则舍入行为与【1. ROUND_UP 模式】相同。
    • 如保留两位:-1.234 => -1.24,-1.230 => -1.24。

5. ROUND_HALF_UP 模式

/**
 * 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;
  • 向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为向上舍入的舍入模式。
  • 这是我们大多数人在小学时就学过的舍入模式(四舍五入),也是我们正常理解的四舍五入模式

6. ROUND_HALF_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;
  • 向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为向下舍入的舍入模式。
  • 和上面一个相反,可以理解为五舍六入

7. ROUND_HALF_EVEN 模式

/**
 * 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;
  • 向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。
  • 此舍入模式也称为“银行家舍入法”,四舍六入,五分两种情况
    • 1.15 => 1.2。
    • 1.25 => 1.2。

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
 * {@code ArithmeticException} is thrown.
 */
public final static int ROUND_UNNECESSARY =  7;
  • 断言请求的操作具有精确的结果,因此不需要舍入
  • 如果对获得精确结果的操作指定此舍入模式,则抛出 ArithmeticException
  • 使用场景较少。

三、参考链接

原文链接:https://blog.csdn.net/weixin_45967584/article/details/136869575

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值