Java BigDecimal add example

In this example working of .add() method is demonstrated in following two constructor forms add(BigDecimal augend) and add(BigDecimal augend, MathContext mc) respectively.

Java BigDecimal add example

In this example working of .add() method is demonstrated in following two constructor forms add(BigDecimal augend) and add(BigDecimal augend, MathContext mc) respectively.

Java BigDecimal add example

Java BigDecimal add example

     

In this example working of .add() method is demonstrated in following two constructor forms add(BigDecimal augend) and add(BigDecimal augend, MathContext mc) respectively. Method returns the addition result of two bigdecimal class objects values. And the returned value is itself a bigdecimal value which means the result generated, has to stored by a bigdecimal object (provided the result is not converted and type casted to nay other type). 

In the example two bigdecimal objects obj_0 and obj_1 values are added and the result is stored in object obj_3. Also the addition result of objects obj_4 & obj_5 are stored in object obj_6. Object obj_6 contains addition result as per context settings defined under MathContext class objects  mc and nc respectively. 

Syntax for using add method : 
  public BigDecimal add(BigDecimal augend),
  public BigDecimal add(BigDecimal augend, MathContext mc) 

  System.out.println(bigDecimal_objectName.add()); 

Scale associated to the result is computed through bigdecimal class max method. .max() method compares the this.object scale with the  scale of the object specified & returns scale which maximum!greater.  

In the example for formating purpose two new classes NumberFormat & Locale are included.
Method throws NumberFormatException if it finds a value other than a integer or double value. 

import java.math.*; // java.math.BigDecimal; //java.math.MathContext;
// //java.math.RoundingMode;
import java.text.*; // java.text.NumberFormat;
import java.util.*; // java.util.Locale;

public class Java_BigDecimal_add {
  public static void main(String args[]) {

  NumberFormat dollar = NumberFormat.getCurrencyInstance(Locale.US);

  char val[] '5''0''0''0' };
  BigDecimal obj_0 = new BigDecimal(val);

  long rng = obj_0.longValueExact();
  BigDecimal obj_1 = new BigDecimal(rng), obj_3 = new BigDecimal(0);

  System.out.println("BigDecimal class '.add(BigDecimal object)' "
  "example\n");
  System.out.println("obj_0 value : "
  + dollar.format(obj_0.doubleValue()));

  System.out.println("obj_1 value : "
  + dollar.format(obj_1.doubleValue()));

  obj_3 = obj_0.add(obj_1);
  System.out.print("Added objects value with .add method : "
  + dollar.format(obj_3));

  System.out.print("\n\nBigDecimal class '.add(BigDecimal "
  "object, MathContext mc)' example\n\n");

  MathContext mc = new MathContext(0, RoundingMode.CEILING)
  nc = new MathContext(0, RoundingMode.DOWN);

  double value = 2000.256;
  String str = "3000.564";

  BigDecimal obj_4 = new BigDecimal(value, mc),
  obj_5 = new BigDecimal(Double.parseDouble(str)),
  obj_6 = new BigDecimal(0);

  System.out.println("obj_4 value : $" (obj_4"\nobj_5 value" +
  " : $" (obj_5));
  obj_6 = obj_4.add(obj_5, nc);
  System.out.println("Added value as per context settings : "
  + dollar.format(obj_6.doubleValue()));

  }
}

Download the source code