Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

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.

Ads

Tutorials   
Java Bigdecimal Java bigdecimal toBigIntegerExact example Java bigdecimal shortValue example Java bigdecimal shortValueExact Java bigdecimal signum example Java bigdecimal stripTrailingZeros example Java bigdecimal subtract Java bigdecimal subtract method example Java bigdecimal toBigInteger example Java bigdecimal toEngineeringString example Java bigdecimal toPlainString example Java bigdecimal toString example Java bigdecimal ulp example Java bigdecimal unscaledValue example Java bigdecimal valueOf double example Java bigdecimal valueOf long example Java bigdecimal valueOf long method Java bigdecimal setScale method example Java bigdecimal setScale example Java bigdecimal scaleByPowerOfTen example Java bigdecimal scale example Java bigdecimal round example Java bigdecimal remainder method example Java bigdecimal remainder example Java bigdecimal precision example Java bigdecimal pow method example Java bigdecimal pow example Java bigdecimal plus method example Java bigdecimal plus example Java bigdecimal negate method example Java bigdecimal negate example Java bigdecimal multiply method example Java bigdecimal multiply example Java BigDecimal movePointRight example Java bigdecimal movePointLeft example Java bigdecimal min example Java bigdecimal max example Java BigDecimal divideAndRemainder example Java BigDecimal divideAndRemainder method example Java BigDecimal divide examples Java BigDecimal divideToIntegral example Java BigDecimal divideToIntegral method example Java BigDecimal doubleValue example Java BigDecimal equals example Java BigDecimal hashCode example Java BigDecimal intValue example Java BigDecimal intValueExact example Java BigDecimal longValue example Java BigDecimal longValueExact example Java BigDecimal abs example
Ads

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

Advertisements

Share on Google+Share on Google+

Java BigDecimal add example

Posted on: November 21, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Java BigDecimal add example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads