📜  Java中的 BigInteger divide() 方法及示例

📅  最后修改于: 2022-05-13 01:55:28.551000             🧑  作者: Mango

Java中的 BigInteger divide() 方法及示例

Java .math.BigInteger.divide(BigInteger val)用于计算两个 BigInteger 的除法。 BigInteger 类内部使用整数数组进行处理,对 BigInteger 对象的操作不如对原语的快。此方法对调用此方法的当前 BigInteger 执行操作,并将 BigInteger 作为参数传递。

句法:

public BigInteger divide(BigInteger val)

参数:此方法接受一个参数val ,该参数是除此 BigInteger 的值。

返回值:此方法返回一个 BigInteger,它将除法 (this / val) 保存在 Integer(非浮点值)中,即,它将结果四舍五入为其底值。

异常:参数 val 不能为 0,否则会抛出算术异常

下面的程序用于说明 BigInteger 的 divide() 方法。

示例 1:

// Java program to demonstrate
// divide() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigInteger object to store result
        BigInteger div;
  
        // Two objects of String created
        // Holds the values to calculate the division
        String input1 = "400000000000000000"
                        + "000000000000000000";
  
        String input2 = "8000000";
  
        // Convert the string input to BigInteger
        BigInteger a
            = new BigInteger(input1);
        BigInteger b
            = new BigInteger(input2);
  
        // Using divide() method
        div = a.divide(b);
  
        // Display the result in BigInteger
        System.out.println("The division of\n"
                           + a + " \nby\n" + b + " "
                           + "\nis\n" + div);
    }
}
输出:
The division of
400000000000000000000000000000000000 
by
8000000 
is
50000000000000000000000000000

示例 2:演示如何对结果进行四舍五入

// Java program to demonstrate
// divide() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigInteger object to store result
        BigInteger div;
  
        // Two objects of String created
        // Holds the values to calculate the division
        String input1 = "456216545";
  
        String input2 = "21255132";
  
        // Convert the string input to BigInteger
        BigInteger a
            = new BigInteger(input1);
        BigInteger b
            = new BigInteger(input2);
  
        // Using divide() method
        div = a.divide(b);
  
        // Display the result in BigInteger
        System.out.println("The division of\n"
                           + a + " \nby\n" + b + " "
                           + "\nis " + div);
  
        double d = Double.parseDouble(input1)
                   / Double.parseDouble(input2);
  
        // Display result in double type
        // To match both the results
        System.out.print("Using double result is " + d);
    }
}
输出:
The division of
456216545 
by
21255132 
is 21
Using double result is 21.46383024109189

示例 3:演示除以 0 时抛出的异常

// Java program to demonstrate
// divide() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigInteger object to store result
        BigInteger div;
  
        // Two objects of String created
        // Holds the values to calculate the division
        String input1 = "456216545"
                        + "452133155";
  
        String input2 = "0";
  
        // Convert the string input to BigInteger
        BigInteger a
            = new BigInteger(input1);
        BigInteger b
            = new BigInteger(input2);
  
        // Using divide() method
        try {
            div = a.divide(b);
  
            // Display the result in BigInteger
            System.out.println("The division of\n"
                               + a + " \nby\n" + b + " "
                               + "\nis\n" + div + "\n");
        }
        catch (ArithmeticException e) {
            System.out.println(e);
        }
    }
}
输出:
java.lang.ArithmeticException: BigInteger divide by zero

参考: Java : Java(Java )