BigDecimal subtract() Method in Java with Examples - GeeksforGeeks

27 May, 2019
The java.math.BigDecimal.subtract(BigDecimal val) is used to calculate the Arithmetic difference of two BigDecimals. This method is used to find the arithmetic difference of large numbers without compromising with the precision of the result. This method performs an operation upon the current BigDecimal by which this method is called and BigDecimal passed as the parameter.
There are two overloads of subtract method available in java which are listed below:
subtract(BigDecimal val)
subtract(BigDecimal val, MathContext mc)
Syntax:
public BigDecimal subtract(BigDecimal val)

Parameters: This method accepts a parameter val which is the value to be subtracted from this BigDecimal.
Return value: This method returns a BigDecimal which holds difference (this – val), and whose scale is max(this.scale(), val.scale()).
Below programs is used to illustrate the subtract() method of BigDecimal.
// Java program to demonstrate// subtract() method of BigDecimal  import java.math.BigDecimal;  public class GFG {    public static void main(String[] args)    {        // BigDecimal object to store result        BigDecimal diff;          // For user input        // Use Scanner or BufferedReader          // Two objects of String created        // Holds the values to calculate the difference        String input1            = "545456468445645468464645";        String input2            = "425645648446468486486452";          // Convert the string input to BigDecimal        BigDecimal a            = new BigDecimal(input1);        BigDecimal b            = new BigDecimal(input2);          // Using subtract() method        diff = a.subtract(b);          // Display the result in BigDecimal        System.out.println("The difference of\n"                           + a + " \nand\n" + b + " "                           + "\nis\n" + diff + "\n");    }}
Output:
The difference of545456468445645468464645and425645648446468486486452is119810819999176981978193
Syntax:
public BigDecimal subtract(BigDecimal val, MathContext mc)

Parameters: This method accepts two parameter, one is val which is the value to be subtracted from this BigDecimal and a mc of type MathContext.
Return value: This method returns a BigDecimal which holds difference (this – val), with rounding according to the context settings.
Below programs is used to illustrate the subtract() method of BigDecimal.
// Java program to demonstrate// subtract() method of BigDecimal  import java.math.*;  public class GFG {    public static void main(String[] args)    {        // BigDecimal object to store result        BigDecimal diff;          // For user input        // Use Scanner or BufferedReader          // Two objects of String created        // Holds the values to calculate the difference        String input1            = "468445645468464645";        String input2            = "4256456484464684864864";          // Convert the string input to BigDecimal        BigDecimal a            = new BigDecimal(input1);        BigDecimal b            = new BigDecimal(input2);          // Set precision to 10        MathContext mc            = new MathContext(10);        // Using subtract() method        diff = a.subtract(b, mc);          // Display the result in BigDecimal        System.out.println("The difference of\n"                           + a + " \nand\n" + b + " "                           + "\nis\n" + diff + "\n");    }}
Output:
The difference of468445645468464645and4256456484464684864864is-4.255988039E+21
References: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#subtract(java.math.BigDecimal)
Java-BigDecimal
Java-Functions
Java-math-package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Stream In Java
Singleton Class in Java
Set in Java
Multithreading in Java
Collections in Java
Queue Interface In Java
LinkedList in Java
Overriding in Java
Constructors in Java