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