Java BigDecimal subtract() Method with Example

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

BigDecimal Class subtract() method

Syntax:

    public BigDecimal subtract(BigDecimal val);
    public BigDecimal subtract(BigDecimal val, MathContext ma_co);
  • subtract() method is available in java.math package.
  • subtract(BigDecimal val) method is used to get a BigDecimal that holds the value subtracted the given parameter (val) from this BigDecimal and its scale is calculated by using max([this BigDecimal.scale()] , [BigDecimal val.scale()]).
  • subtract(BigDecimal val, MathContext ma_co) method is used to get a BigDecimal that holds the value substracted the given parameter (val) from this BigDecimal based on the given MathContext settings.
  • These methods may throw an exception at the time of substracting 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, subtract(BigDecimal val),
    • BigDecimal val – represents the object is to be substracted from this BigDecimal.
  • In the first case, subtract(BigDecimal val, MathContext ma_co),
    • BigDecimal 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 value substracted the given parameter from this object.
  • In the second case, it returns the value substracted the given parameter from this object with rounding "NECESSARY".

Example:

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

import java.math.*;

public class SubstractOfBD {
    public static void main(String args[]) {
        // Initialize two variables - val,
        // and str 
        int val = 120;
        String str = "2.357";

        // Initialize two BigDecimal objects and
        // one MathContext
        BigDecimal b_dec1 = new BigDecimal(val);
        BigDecimal b_dec2 = new BigDecimal(str);
        MathContext ma_co = new MathContext(5, RoundingMode.CEILING);

        // substracts the given BigDecimal b_dec2 from
        // this BigDecimal b_dec1 and store it in a
        // variable named sub_val
        BigDecimal sub_val = b_dec1.subtract(b_dec2);
        System.out.println("b_dec1.subtract(b_dec2): " + sub_val);

        // substracts the given BigDecimal b_dec2 from
        // this BigDecimal b_dec1 and store it in a
        // variable named sub_val
        sub_val = b_dec1.subtract(b_dec2, ma_co);
        System.out.println("b_dec1.subtract(b_dec2,ma_co): " + sub_val);
    }
}

Output

b_dec1.subtract(b_dec2): 117.643
b_dec1.subtract(b_dec2,ma_co): 117.65


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.