Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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. 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

                         

» View all related tutorials
Related Tags: c class object method decimal gd trac value return this invoke if ie example work bigdecimal values exam ci bi

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.