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
相關用法
- Java BigInteger add()用法及代碼示例
- Java BigInteger gcd()用法及代碼示例
- Java BigInteger multiply()用法及代碼示例
- Java BigInteger isProbablePrime()用法及代碼示例
- Java BigInteger sqrtAndRemainder()用法及代碼示例
- Java 8 BigInteger divideAndRemainder()用法及代碼示例
- Java 8 BigInteger byteValueExact()用法及代碼示例
- Java 8 BigInteger longValueExact()用法及代碼示例
- Java 8 BigInteger shortValueExact()用法及代碼示例
- Java BigInteger intValueExact()用法及代碼示例
- Java BigInteger nextProbablePrime()用法及代碼示例
- Java BigInteger subtract()用法及代碼示例
- Java BigDecimal divide()用法及代碼示例
- Java IntMath.divide(int, int, RoundingMode)用法及代碼示例
- Java LongMath.divide(long, long, RoundingMode)用法及代碼示例
注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 BigInteger divide() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。