📜  Java中的 BigDecimal multiply() 方法

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

Java中的 BigDecimal multiply() 方法

  1. Java.math.BigDecimal.multiply( BigDecimal multiplicand )是Java中的一个内置方法,它返回一个 BigDecimal,其值为 (this × multiplicand),其比例为 (this.scale() + multiplicand.scale())。

    句法:

    public BigDecimal multiply(BigDecimal multiplicand)
    

    参数:此方法接受 BigDecimal 类型的单个参数被乘数,它指的是要与此 BigDecimal 相乘的值。

    返回值:此方法返回一个 BigDecimal,其值为this * multiplicand

    下面的程序说明了上述方法的工作:
    方案一:

    // Java program to demonstrate the
    // multiply() method
      
    import java.math.*;
      
    public class gfg {
      
        public static void main(String[] args)
        {
      
            // Assign two BigDecimal objects
            BigDecimal b1 = new BigDecimal("54.2");
            BigDecimal b2 = new BigDecimal("14.20");
      
            // Multiply b1 with b2 and assign result to b3
            BigDecimal b3 = b1.multiply(b2);
      
            // Print b3 value
            System.out.println("Multiplication is " + b3);
        }
    }
    
    输出:
    Multiplication is 769.640
    

    方案二:

    // Java program to demonstrate the
    // multiply() method
      
    import java.math.*;
      
    public class Gfg {
      
        public static void main(String[] args)
        {
      
            // Assign two BigDecimal objects
            BigDecimal b1 = new BigDecimal("-54.2");
            BigDecimal b2 = new BigDecimal("14.20");
      
            // Multiply b1 with b2 and assign result to b3
            BigDecimal b3 = b1.multiply(b2);
      
            // Print b3 value
            System.out.println("Multiplication is " + b3);
        }
    }
    
    输出:
    Multiplication is -769.640
    
  2. Java.math.BigDecimal.multiply( BigDecimal multiplicand, MathContext mc )是Java中的一个内置方法,它返回一个 BigDecimal,其值为 (this × multiplicand),根据上下文设置进行舍入。

    句法:

    public BigDecimal multiply(BigDecimal multiplicand, MathContext mc)
    

    参数:此方法接受两个参数:

    • multiplicand – 这是 BigDecimal 类型,指的是要与此 BigDecimal 相乘的值。
    • mc – 这指的是四舍五入的上下文,即四舍五入到小数位的值。

    返回值:此方法返回一个 BigDecimal,其值为 this * 被乘数,必要时四舍五入。

    下面的程序演示了该方法:

    方案一:

    // Java program to demonstrate the
    // multiply() method
    import java.math.*;
      
    public class Gfg {
      
        public static void main(String[] args)
        {
      
            // 4 precision
            MathContext m = new MathContext(4); 
      
            // Assign value to BigDecimal objects
            BigDecimal b1 = new BigDecimal("5.99");
            BigDecimal b2 = new BigDecimal("4.6");
      
            // Multiply b1 with b2 using m
            BigDecimal b3 = b1.multiply(b2, m);
      
            // Print b3 value
            System.out.println("Multiplication is " + b3);
        }
    }
    
    输出:
    Multiplication is 27.55
    

    方案二:

    // Java program to demonstrate the
    // multiply() method
    import java.math.*;
      
    public class Gfg {
      
        public static void main(String[] args)
        {
      
            // 4 precision
            MathContext m = new MathContext(4); 
      
            // Assign value to BigDecimal objects
            BigDecimal b1 = new BigDecimal("-5.99");
            BigDecimal b2 = new BigDecimal("4.6");
      
            // Multiply b1 with b2 using m
            BigDecimal b3 = b1.multiply(b2, m);
      
            // Print b3 value
            System.out.println("Multiplication is " + b3);
        }
    }
    
    输出:
    Multiplication is -27.55
    

参考: https: Java Java.math.BigDecimal)