Multiply one BigDecimal to another BigDecimal in Java

Java 8Object Oriented ProgrammingProgramming

Use the multiply() method to multiply one BigDecimal to another in Java. This method returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).

The following is an example −

Example

 Live Demo

import java.math.BigDecimal;
public class Demo {
   public static void main(String[] argv) throws Exception {
      BigDecimal val1 = new BigDecimal("375789755.345778656");
      BigDecimal val2 = new BigDecimal("525678755.155778656");
      System.out.println("Value 1 = "+val1);
      System.out.println("Value 2 = "+val2);
      val2 = val2.multiply(val1);
      System.out.println("Result (Multiplication) = "+val2);
   }
}

Output

Value 1 = 375789755.345778656
Value 2 = 525678755.155778656
Result (Multiplication) = 197544690790463541.417987494305166336

Let us see another example −

Example

 Live Demo

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigDecimal bg1, bg2, bg3;
      bg1 = new BigDecimal("58.98");
      bg2 = new BigDecimal("78.89");
      bg3 = bg1.multiply(bg2);
      String str = "Multiplication = " +bg3;
      System.out.println( str );
   }
}

Output

Multiplication = 4652.9322
raja
Published on 20-Dec-2018 11:15:24
Advertisements