當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。