Thursday, September 4, 2025
HomeLanguagesJavaBigDecimal multiply() Method in Java

BigDecimal multiply() Method in Java

  1. The java.math.BigDecimal.multiply(BigDecimal multiplicand) is an inbuilt method in java that returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).

    Syntax:

    public BigDecimal multiply(BigDecimal multiplicand)
    

    Parameters: This method accepts a single parameter multiplicand of BigDecimal type which refers to the Value to be multiplied by this BigDecimal.

    Return value: This method returns a BigDecimal whose value this * multiplicand.

    Below program illustrates the working of the above mentioned method:
    Program 1:




    // 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);
        }
    }

    
    
    Output:

    Multiplication is 769.640
    

    Program 2:




    // 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);
        }
    }

    
    
    Output:

    Multiplication is -769.640
    
  2. The java.math.BigDecimal.multiply(BigDecimal multiplicand, MathContext mc) is an inbuilt method in Java that returns a BigDecimal whose value is (this × multiplicand), with rounding according to the context settings.

    Syntax:

    public BigDecimal multiply(BigDecimal multiplicand, MathContext mc)
    

    Parameters: This method accepts two parameters:

    • multiplicand – This is of BigDecimal type and refers to the value to be multiplied by this BigDecimal.
    • mc – This refers to the context of rounding i.e., up to what decimal place the value is to be rounded off.

    Return value: This method returns a BigDecimal whose value this * multiplicand, rounded as necessary.

    Program below demonstrates the method:

    Program 1:




    // 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);
        }
    }

    
    
    Output:

    Multiplication is 27.55
    

    Program 2:




    // 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);
        }
    }

    
    
    Output:

    Multiplication is -27.55
    

Reference:https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#multiply(java.math.BigDecimal)

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6632 POSTS0 COMMENTS
Nicole Veronica
11800 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11860 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS