Wednesday, July 3, 2024
HomeLanguagesJavaBigDecimal add() Method in Java with Examples

BigDecimal add() Method in Java with Examples

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)

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments