- 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() methodimportjava.math.*;publicclassgfg {publicstaticvoidmain(String[] args){// Assign two BigDecimal objectsBigDecimal b1 =newBigDecimal("54.2");BigDecimal b2 =newBigDecimal("14.20");// Multiply b1 with b2 and assign result to b3BigDecimal b3 = b1.multiply(b2);// Print b3 valueSystem.out.println("Multiplication is "+ b3);}}chevron_rightfilter_noneOutput:
Multiplication is 769.640
Program 2:
// Java program to demonstrate the// multiply() methodimportjava.math.*;publicclassGfg {publicstaticvoidmain(String[] args){// Assign two BigDecimal objectsBigDecimal b1 =newBigDecimal("-54.2");BigDecimal b2 =newBigDecimal("14.20");// Multiply b1 with b2 and assign result to b3BigDecimal b3 = b1.multiply(b2);// Print b3 valueSystem.out.println("Multiplication is "+ b3);}}chevron_rightfilter_noneOutput:Multiplication is -769.640
-
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() methodimportjava.math.*;publicclassGfg {publicstaticvoidmain(String[] args){// 4 precisionMathContext m =newMathContext(4);// Assign value to BigDecimal objectsBigDecimal b1 =newBigDecimal("5.99");BigDecimal b2 =newBigDecimal("4.6");// Multiply b1 with b2 using mBigDecimal b3 = b1.multiply(b2, m);// Print b3 valueSystem.out.println("Multiplication is "+ b3);}}chevron_rightfilter_noneOutput:Multiplication is 27.55
Program 2:
// Java program to demonstrate the// multiply() methodimportjava.math.*;publicclassGfg {publicstaticvoidmain(String[] args){// 4 precisionMathContext m =newMathContext(4);// Assign value to BigDecimal objectsBigDecimal b1 =newBigDecimal("-5.99");BigDecimal b2 =newBigDecimal("4.6");// Multiply b1 with b2 using mBigDecimal b3 = b1.multiply(b2, m);// Print b3 valueSystem.out.println("Multiplication is "+ b3);}}chevron_rightfilter_noneOutput:Multiplication is -27.55
Reference:https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#multiply(java.math.BigDecimal)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- BigDecimal min() Method in Java
- BigDecimal max() Method in Java
- BigDecimal abs() Method in Java
- BigDecimal valueOf() Method in Java
- BigDecimal byteValueExact() Method in Java
- BigDecimal doubleValue() Method in Java
- BigDecimal ulp() Method in Java
- BigDecimal toBigIntegerExact() Method in Java
- BigDecimal toBigInteger() Method in Java
- BigDecimal stripTrailingZeros() Method in Java
- BigDecimal equals() Method in Java
- BigDecimal shortValueExact() Method in Java
- BigDecimal signum() Method in Java
- BigDecimal longValue() Method in Java
- BigDecimal hashCode() Method in Java
- BigDecimal precision() Method in Java
- BigDecimal intValue() Method in Java
- BigDecimal longValueExact() Method in Java
- BigDecimal intvalueExact() Method in Java
- BigDecimal round() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

