ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Java BigDecimal multiply() Method with Example

BigDecimal Class multiply() method: Here, we are going to learn about the multiply() method of BigDecimal Class with its syntax and example.
Submitted by Preeti Jain, on May 05, 2020

BigDecimal Class multiply() method

Syntax:

    public BigDecimal multiply(BigDecimal m_val);
    public BigDecimal multiply(BigDecimal m_val, MathContext ma_co);
  • multiply() method is available in java.math package.
  • multiply(BigDecimal m_val) method is used get a BigDecimal that holds the value multiplied this BigDecimal by the given BigDecimal and its scale is calculated by using ([this BigDecimal.scale()] * [BigDecimal m_val.scale()]).
  • multiply(BigDecimal m_val, MathContext ma_co) method is used to get a BigDecimal that holds the value multiplied this BigDecimal by the given BigDecimal based on the given MathContext settings.
  • These methods may throw an exception at the time of multiplied an object.
    ArithmeticException: This exception may throw when the result is not accurate and set the rounding mode "UNNECESSARY".
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, multiply(BigDecimal m_val),
    • BigDecimal m_val – represents the object is used to multiply this BigDecimal object.
  • In the first case, multiply(BigDecimal m_val, MathContext ma_co),
    • BigDecimal m_val – Similar as defined in the first case.
    • MathContext ma_co – represents the context setting to use in rounding.

Return value:

In both the cases, the return type of the method is BigDecimal,

  • In the first case, it returns the multiplied result of both the objects without rounding.
  • In the second case, it returns the multiplied result of both the objects with rounding if required.

Example:

// Java program to demonstrate the example 
// of multiply() method of BigDecimal

import java.math.*;

public class MultiplyOfBD {
    public static void main(String args[]) {
        // Initialize three variables val1,
        // val2 and mul_val 
        String val1 = "10.30";
        int val2 = 4;
        String mul_val = "5";

        // Initialize three BigDecimal objects and
        // one MathContext
        BigDecimal b_dec1 = new BigDecimal(val1);
        BigDecimal b_dec2 = new BigDecimal(val2);
        BigDecimal b_dec3 = new BigDecimal(mul_val);
        MathContext ma_co = new MathContext(2, RoundingMode.CEILING);

        System.out.println("multiply(BigDecimal): ");

        // multiplies this BigDecimal (b_dec2) by the
        // given BigDecimal (b_dec3) and return the result
        BigDecimal mul_res = b_dec2.multiply(b_dec3);
        System.out.println("b_dec2.multiply(b_dec3): " + mul_res);

        System.out.println(" ");

        System.out.println("multiply(BigDecimal,MathContext): ");

        // multiplies this BigDecimal (b_dec1) by the
        // given BigDecimal (b_dec3) and return the result based
        // on the given context settings
        mul_res = b_dec1.multiply(b_dec3, ma_co);
        System.out.println("b_dec1.multiply(b_dec3, ma_co): " + mul_res);
    }
}

Output

multiply(BigDecimal): 
b_dec2.multiply(b_dec3): 20
 
multiply(BigDecimal,MathContext): 
b_dec1.multiply(b_dec3, ma_co): 52
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.