Java.math.BigDecimal.divide()方法

方法的声明:

 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;

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

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

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

补充其他方法:

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则向下舍

  • 11
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Java中的`BigDecimal`是用于高精度计算的类。它提供了大数的精确计算,适用于处理需要精确小数位数的情况,例如金融计算或科学计算。 `BigDecimal`可以处理任意精度的数值,并提供了各种运算方法,如加法、减法、乘法和除法等。它还支持舍入模式和比较操作。 使用`BigDecimal`时需要注意以下几点: 1. `BigDecimal`对象是不可变的,每次进行运算都会返回一个新的`BigDecimal`对象。 2. 构造一个`BigDecimal`对象时,可以使用字符串、整型、浮点型等作为参数。 3. 进行除法运算时,需要指定除法运算规则和舍入模式。 4. 要比较两个`BigDecimal`对象的大小,可以使用`compareTo()`方法。 下面是一个简单的示例代码,演示了如何使用`BigDecimal`进行精确计算: ```java import java.math.BigDecimal; public class BigDecimalExample { public static void main(String[] args) { BigDecimal num1 = new BigDecimal("10.5"); BigDecimal num2 = new BigDecimal("5.2"); // 加法 BigDecimal sum = num1.add(num2); System.out.println("Sum: " + sum); // 减法 BigDecimal difference = num1.subtract(num2); System.out.println("Difference: " + difference); // 乘法 BigDecimal product = num1.multiply(num2); System.out.println("Product: " + product); // 除法 BigDecimal quotient = num1.divide(num2, 2, BigDecimal.ROUND_HALF_UP); System.out.println("Quotient: " + quotient); // 比较大小 int comparison = num1.compareTo(num2); if (comparison > 0) { System.out.println("num1 is greater than num2"); } else if (comparison < 0) { System.out.println("num1 is less than num2"); } else { System.out.println("num1 is equal to num2"); } } } ``` 希望这个简单的解释和示例能帮到你!如果有任何其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值