当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java BigInteger divide()用法及代码示例


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

用法:

public BigInteger divide(BigInteger val)

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


返回值:此方法返回一个BigInteger,它以Integer(非浮点值)形式保留除(this /val),即,将结果四舍五入到其底值。

异常:参数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

参考: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#divide(java.math.BigInteger)



相关用法


注:本文由纯净天空筛选整理自Rajnis09大神的英文原创作品 BigInteger divide() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。