java.math.BigInteger.multiply(BigInteger val)用於計算兩個BigInteger的乘法。由於BigInteger類內部使用整數數組進行處理,因此對BigInteger對象的操作不如對基元進行的操作快。
用法:
public BigInteger multiply(BigInteger val)
參數:此方法接受參數val,該參數是要與該BigInteger相乘的值。
返回值:此方法返回一個保存乘法(此* val)的BigInteger。
下麵的程序用於說明BigInteger的multiply()方法。
示例1:
// Java program to demonstrate
// multiply() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// BigInteger object to store result
BigInteger mult;
// For user input
// Use Scanner or BufferedReader
// Two objects of String created
// Holds the values to calculate the multiplication
String input1 = "012345678901234567"
+ "654632498739473";
String input2 = "0123456789012345"
+ "61247612748612746";
// Convert the string input to BigInteger
BigInteger a
= new BigInteger(input1);
BigInteger b
= new BigInteger(input2);
// Using multiply() method
mult = a.multiply(b);
// Display the result in BigInteger
System.out.println("The multiplication of\n"
+ a + " \nand\n" + b + " "
+ "\nis\n" + mult);
}
}
輸出:
The multiplication of
12345678901234567654632498739473
and
12345678901234561247612748612746
is
152415787532388282591353462245536419067346861445890674421122858
示例2:
// Java program to demonstrate
// multiply() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// BigInteger object to store result
BigInteger mult;
// For user input
// Use Scanner or BufferedReader
// Two objects of String created
// Holds the values to calculate the multiplication
String input1 = "012345678901234567"
+ "8901234567890123"
+ "4567890123456789"
+ "0123456789012345"
+ "6789012345678901"
+ "654632498739473";
String input2 = "0123456789012345"
+ "6789012345678901"
+ "2345678901234567"
+ "8901234567890123"
+ "4567890123456789"
+ "61247612748612746";
// Convert the string input to BigInteger
BigInteger a
= new BigInteger(input1);
BigInteger b
= new BigInteger(input2);
// Using multiply() method
mult = a.multiply(b);
// Display the result in BigInteger
System.out.println("The multiplication of\n"
+ a + " \nand\n" + b + " "
+ "\nis\n" + mult + "\n");
// Using double to hold the result
double d = Double.parseDouble(mult.toString());
// Display the result in double
System.out.println("Using double, multiplication is "
+ d);
}
}
輸出:
The multiplication of
123456789012345678901234567890123456789012345678901234567890123456789012345678901654632498739473
and
123456789012345678901234567890123456789012345678901234567890123456789012345678961247612748612746
is
15241578753238836750495351562566681945008382873376009755225118122311263526910008985036532509972574264073578551235889967606442208008929541925721486305055001841778994861500543809890674421122858Using double, multiplication is 1.5241578753238838E190
從上麵的示例中可以明顯看出,使用BigInteger時數據是完全精確的。
相關用法
- Java BigInteger gcd()用法及代碼示例
- Java BigInteger add()用法及代碼示例
- Java BigInteger subtract()用法及代碼示例
- Java 8 BigInteger byteValueExact()用法及代碼示例
- Java BigInteger intValueExact()用法及代碼示例
- Java 8 BigInteger shortValueExact()用法及代碼示例
- Java BigInteger isProbablePrime()用法及代碼示例
- Java 8 BigInteger longValueExact()用法及代碼示例
- Java BigInteger divide()用法及代碼示例
- Java BigInteger nextProbablePrime()用法及代碼示例
- Java BigInteger sqrtAndRemainder()用法及代碼示例
- Java 8 BigInteger divideAndRemainder()用法及代碼示例
- Java BigInteger or()用法及代碼示例
- Java BigInteger pow()用法及代碼示例
- Java BigInteger abs()用法及代碼示例
注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 BigInteger multiply() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。