Skip to content
Related Articles

Related Articles

BigDecimal add() Method in Java with Examples
  • Last Updated : 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)

add(BigDecimal val)

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 of
545456468445645468464645
and
4256456484464684864864
is
549712924930110153329509

add(BigDecimal val, MathContext mc)

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 of
9854228445645468464645
and
4252145764464684864864
is
1.410637421E+22

References: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#add(java.math.BigDecimal)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :