BigDecimal add() Method in Java with Examples - GeeksforGeeks

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

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

Parameters: This method accepts two parameter, one is val which is the value to be added to this BigDecimal and a mc of type MathContext.
Return value: This method returns a BigDecimal which holds sum (this + val), with rounding according to the context settings. If either number is zero and the precision setting is nonzero then the other number, rounded if necessary, is used as the result.
Below programs is used to illustrate the add() method of BigDecimal.
// Java program to demonstrate// add() method of BigDecimal  import java.math.*;  public class GFG {    public static void main(String[] args)    {        // BigDecimal object to store the result        BigDecimal sum;          // For user input        // Use Scanner or BufferedReader          // Two objects of String created        // Holds the values to calculate the sum        String input1            = "9854228445645468464645";        String input2            = "4252145764464684864864";          // 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 add() method        sum = a.add(b, mc);          // Display the result in BigDecimal        System.out.println("The sum of\n"                           + a + " \nand\n" + b + " "                           + "\nis\n" + sum + "\n");    }}
Output:
The sum of9854228445645468464645and4252145764464684864864is1.410637421E+22
References: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#add(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
Overriding in Java
LinkedList in Java
Collections in Java
Initializing a List in Java
Queue Interface In Java
Multithreading in Java