📜  Java中的 BigInteger multiply() 方法及示例

📅  最后修改于: 2022-05-13 01:55:13.151000             🧑  作者: Mango

Java中的 BigInteger multiply() 方法及示例

Java .math.BigInteger.multiply(BigInteger val)用于计算两个 BigInteger 的乘积。由于 BigInteger 类在内部使用整数数组进行处理,因此对 BigInteger 对象的操作不如对原语的快。

句法:

public BigInteger multiply(BigInteger val)

参数:此方法接受参数val ,该参数是要与此 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);
    }
}

输出:

示例 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);
    }
}

输出:

从上面的例子可以看出,当使用 BigInteger 时,数据是完全精确的。

参考: Java : Java(Java )