Java
  Home arrow Java arrow Page 3 - How the BigDecimal Class Helps Java Get it...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

How the BigDecimal Class Helps Java Get its Arithmetic Right
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2006-02-02

    Table of Contents:
  • How the BigDecimal Class Helps Java Get its Arithmetic Right
  • The math problems in the Invoice application
  • How to use the BigDecimal class
  • How to use BigDecimal arithmetic in the Invoice application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    How the BigDecimal Class Helps Java Get its Arithmetic Right - How to use the BigDecimal class


    (Page 3 of 4 )



    The BigDecimal class is designed to solve two types of problems that are associated with floating-point numbers. First, the BigDecimal class can be used to exactly represent decimal numbers. Second, it can be used to work with numbers that have more than 16 significant digits. If you haven’t ever used this class, it’s one that you should master and use for many business applications.

    The constructors and methods of the BigDecimal class

    Figure 3 summarizes a few of the constructors that you can use with the BigDecimal class. These constructors accept an int, double, long, or string argument and create a BigDecimal object from it. Because floating-point numbers are limited to 16 significant digits and because these numbers don’t always represent decimal numbers exactly, it’s often best to construct BigDecimal objects from strings rather than doubles.

    Once you create a BigDecimal object, you can use its methods to work with the data. In this figure, for example, you can see some of the BigDecimal methods that are most useful in business applications. Here, the add, subtract, multiply, and divide methods let you perform those operations. The compareTo method lets you compare the values in two BigDecimal objects. And the toString method converts the value of a BigDecimal object to a string.

    This figure also includes the setScale method, which lets you set the number of decimal places (scale) for the value in a BigDecimal object as well as the rounding mode. For example, you can use the setScale method to return a number that’s rounded to two decimal places like this:

      salesTax = salesTax.setScale(2, RoundingMode.HALF_UP);

    In this example, RoundingMode.HALF_UP is a value in the RoundingMode enumeration that’s summarized in this figure. The scale and rounding mode arguments work the same for the divide method.

    In case you aren’t familiar with enumerations, which are new to Java 1.5, they are similar to classes. For our purposes right now, you can code the rounding mode as HALF_UP because it provides the type of rounding that is normal for business applications. However, you need to import the RoundingMode enumeration at the start of the application unless you want to qualify the rounding mode like this:

      java.math.RoundingMode.HALF_UP

    If you look at the API documentation for the BigDecimal class, you’ll see that it provides several other methods that you may want to use. This class also provides many other features that you may want to become more familiar with. But the constructors and methods in this figure will get you started right.

    The BigDecimal class

      java.math.BigDecimal

    Figure 3 The constructors and methods for the BigDecimal class

    Constructors of the BigDecimal class

    Methods of the BigDecimal class

    The RoundingMode enumeration

      java.math.RoundingMode

    Two of the values in the RoundingMode enumeration

      

    Description

    • The BigDecimal class provides a way to perform accurate decimal calculations in Java. It also provides a way to store numbers with more than 16 significant digits.
    • You can pass a BigDecimal object to the format method of a NumberFormat object, but NumberFormat objects limit the results to 16 significant digits.

       

    More Java Articles
    More By Murach Publishing


       · This article is an excerpt from the book "Murach's Beginning Java 2, JDK 5,"...
       · It wasn't until I realized BigDecimal does arithmetic in base 10 - not base 2 - that...
     

    Buy this book now. This article is excerpted from the book Murach's Beginning Java 2, JDK 5, written by Doug Lowe, Joel Murach, and Andrea Steelman (Murach Publishing, 2005; ISBN: 1890774294). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway