ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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
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.