Java BigIntegerMath divide()

Java BigIntegerMath divide()

Guava的BigIntegerMath类的 divide(BigInteger p, BigInteger q, RoundingMode mode) 方法返回p除以q的结果,使用指定的RoundingMode进行舍入。

语法

public static BigInteger divide(BigInteger p,
                                BigInteger q,
                                RoundingMode mode)

参数。此方法需要以下参数。

  • p : BigInteger分红
  • q : BigInteger除数
  • mode : 计算p和q的除法的舍入模式。

返回值。该方法返回p除以q的结果,使用指定的四舍五入模式。

异常: 如果q == 0,或者mode == UNNECESSARY和a不是b的整数倍,该方法会抛出 ArithmeticException

Enum RoundingMode

枚举常数 描述
CEILING 四舍五入模式,朝向正无穷大。
DOWN 向零舍入的舍入模式。
FLOOR 四舍五入到负无穷大的模式。
HALF_DOWN 向 “最近的邻居 “舍入的舍入模式,除非两个邻居的距离相等,在这种情况下向下舍入。
HALF_EVEN 向 “最近的邻居 “舍入的舍入模式,除非两个邻居都是等距离,在这种情况下,向偶数邻居舍入。
HALF_UP 向 “最近的邻居 “舍入的舍入模式,除非两个邻居都是等距离,在这种情况下,向上舍入。
UNNECESSARY 四舍五入模式,断言请求的操作有一个精确的结果,因此不需要四舍五入。
UP 舍入模式,从零开始舍入。

下面的例子说明了 BigIntegerMath.divide() 方法。

例1 :

// Java code to show implementation of
// divide(BigInteger p, BigInteger q, RoundingMode mode)
// method of Guava's BigIntegerMath class
  
import java.math.*;
import com.google.common.math.BigIntegerMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
        BigInteger dividend1 = BigInteger.valueOf(55);
        BigInteger divisor1 = BigInteger.valueOf(10);
  
        // Using divide()
        // method of Guava's BigIntegerMath class
        BigInteger
            quotient1
            = BigIntegerMath
                  .divide(dividend1,
                          divisor1,
                          RoundingMode.HALF_DOWN);
  
        System.out.println(dividend1 + " divided by "
                           + divisor1
                           + " with HALF_DOWN rounding mode: "
                           + quotient1);
  
        BigInteger dividend2 = BigInteger.valueOf(55);
        BigInteger divisor2 = BigInteger.valueOf(10);
  
        // Using divide()
        // method of Guava's BigIntegerMath class
        BigInteger
            quotient2
            = BigIntegerMath
                  .divide(dividend2,
                          divisor2,
                          RoundingMode.CEILING);
  
        System.out.println(dividend2 + " divided by "
                           + divisor2
                           + " with CEILING rounding mode: "
                           + quotient2);
    }
}

输出。

55 divided by 10 with HALF_DOWN rounding mode: 5
55 divided by 10 with CEILING rounding mode: 6

例2 :

// Java code to show implementation of
// divide(BigInteger p, BigInteger q, RoundingMode mode)
// method of Guava's BigIntegerMath class
  
import java.math.*;
import com.google.common.math.BigIntegerMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
  
        try {
  
            BigInteger dividend1 = BigInteger.valueOf(25);
            BigInteger divisor1 = BigInteger.valueOf(0);
  
            // Using divide()
            // method of Guava's BigIntegerMath class
            // This should raise "ArithmeticException"
            // as divisor1 = 0
            BigInteger
                quotient1
                = BigIntegerMath
                      .divide(dividend1,
                              divisor1,
                              RoundingMode.HALF_DOWN);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

输出。

Exception: java.lang.ArithmeticException: / by zero

参考资料: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/math/BigIntegerMath.html#divide-java.math.BigInteger-java.math.BigInteger-java.math.RoundingMode-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程