Java BigInteger multiply()方法及示例
java.math.BigInteger .multiply(BigInteger val) 用于计算两个BigIntegers的乘法。由于BigInteger类内部使用整数数组进行处理,因此对BigInteger对象的操作不如对基数的操作快。
语法
public BigInteger multiply(BigInteger val)
参数: 该方法接受一个参数val,即要与这个BigInteger相乘的值。
返回值: 该方法返回一个BigInteger,该BigInteger持有乘法(this * val)。
下面的程序用来说明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
15241578753238836750495351562566681945008382873376009755225118122311263526910008985036532509972574264073578551235889967606442208008929541925721486305055001841778994861500543809890674421122858
Using double, multiplication is 1.5241578753238838E190
从上面的例子可以看出,使用BigInteger时,数据是完全精确的。
参考: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#multiply(java.math.BigInteger)