BigDecimal的8种Rounding Modes

向上取整,正数加一,负数减一,也就是当失去一个精度时,增加一个精度

1.1变为2  ,1.8变为2 , -1.1变为-2,-1.8变为-2

 public final static int ROUND_UP = 0;

 向下取整,正数减一,负数加一,也就是当失去一个精度时,减少一个精度

1.1变为1  ,1.8变为1 , -1.1变为-1,-1.8变为-1

 public final static int ROUND_DOWN = 1;

向正无穷方向取整,CEILING天花板的意思

1.1变为2  ,1.8变为2 , -1.1变为-1,-1.8变为-1

  public final static int ROUND_CEILING = 2;

向负无穷方向取整,FLOOR地板的意思

1.1变为1  ,1.8变为1 , -1.1变为-2,-1.8变为-2

 public final static int ROUND_FLOOR = 3;

 四舍五入模式。

1.5变为2  ,1.8变为2 , 1.3变为1 ,-1.1变为-1,-1.5变为-2

public final static int ROUND_HALF_UP = 4;

五舍六入模式。

1.5变为1  ,1.6变为2 , 1.3变为1 ,-1.1变为-1,-1.5变为-1,-1.6变为-2

 public final static int ROUND_HALF_DOWN = 5;

 四舍六入五考虑,五后非零就进一,五后为零看奇偶,五前为偶应舍去,五前为奇要进一。

 public final static int ROUND_HALF_EVEN = 6;

  舍入模式可以断言所请求的操作具有准确的结果,因此不需要舍入。如果在产生不精确结果的操作上指定了这种舍入模式,则会引发ArithmeticException

 public final static int ROUND_UNNECESSARY = 7;

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

你可能感兴趣的:(java,开发语言)