Java.math.BigDecimal.divide()方法[亲测有效]

Java.math.BigDecimal.divide()方法[亲测有效]方法的声明:publicBigDecimaldivide(BigDecimaldivisor,intscale,introundingMode)参数解析:divisor是被除数,scale是精确度,roundingMode是结果的取值方式。返回值:返回的结果也是一个BigDecimal测试案例:同时解释一下roundingMode的五种模式案例1…

大家好,欢迎来到IT知识分享网。

方法的声明:

 public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)

参数解析:

divisor是除数,scale是精确度,roundingMode是结果的取值方式。

返回值:

返回的结果也是一个BigDecimal

测试案例:

同时解释一下roundingMode的五种模式

案例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;

Java.math.BigDecimal.divide()方法[亲测有效]

案例2:

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

Java.math.BigDecimal.divide()方法[亲测有效]

案例3:

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

Java.math.BigDecimal.divide()方法[亲测有效]

案例4:

Java.math.BigDecimal.divide()方法[亲测有效]

案例:

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

Java.math.BigDecimal.divide()方法[亲测有效]

补充其他方法:

BigDecimal.setScale()方法用于格式化小数点
setScale(1)表示保留一位小数,默认用四舍五入方式 
setScale(1,BigDecimal.ROUND_DOWN)直接删除多余的小数位,如2.35会变成2.3 
setScale(1,BigDecimal.ROUND_UP)进位处理,2.35变成2.4 
setScale(1,BigDecimal.ROUND_HALF_UP)四舍五入,2.35变成2.4

setScaler(1,BigDecimal.ROUND_HALF_DOWN)四舍五入,2.35变成2.3,如果是5则向下舍

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/26418.html

(0)
上一篇 2023-05-06 17:00
下一篇 2023-05-08 16:00

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信