Java
  Home arrow Java arrow 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  
Actuate Whitepapers 
Moblin 
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
     
    Iron Speed
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    How the BigDecimal Class Helps Java Get its Arithmetic Right


    (Page 1 of 4 )

    If you use Java for simple business arithmetic, you might be seeing some errors. It's not your fault, it's a floating-point problem -- and this article explains how to use Java's BigDecimal class to fix it. It 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).

    When you use Java for simple business arithmetic, you may be surprised to discover that Java doesn’t always produce the right answers. If, for example, you use the double data type for an invoice’s subtotal, sales tax, and total, your arithmetic expressions may deliver inaccurate results. I’ll illustrate this in a moment.

    The problem is that floating-point numbers can’t represent all decimal numbers with complete accuracy. Then, when you round the results to two decimal places, you can get errors. The best solution in a case like this is to use Java’s BigDecimal class, and that’s what you’ll learn to do in this document.

    An Invoice application

    To illustrate the math problems that are common when floating-point values are used for business applications, figure 1 presents a simple console application that calculates several values after the user enters an invoice subtotal. You can see the results for one user entry in this figure. This time, the arithmetic is correct.

    The code for the application

    Figure 1 also presents all of the Java code for this application. Here, the shaded code identifies the double values and arithmetic expressions that are used to do the math that this application requires. After those statements are executed, the results are given percent and currency formats, which round the results. Then, the results are displayed on the console.

    You might notice that this application uses the new Scanner class that became available with Java 1.5. You might also notice that this application doesn’t provide for the exception that’s thrown if the user doesn’t enter a valid number at the console. Even in this simple form, though, the application will illustrate the math problems that are common with floating-point arithmetic.

    The console for the formatted Invoice application


    Figure 1.  An Invoice application that will illustrate some arithmetic problems

    The code for the formatted Invoice application

    import java.util.Scanner;
    import java.text.NumberFormat;
    public class InvoiceApp
    {
        public static void main(String[] args)
        { 
            /
    / create a Scanner object and start while loop
            Scanner sc = new Scanner(System.in);
            String choice = "y";
            while (choice.equalsIgnoreCase("y"))
            {
               
    // get the input from the user
                System.out.print("Enter subtotal: ");
                 double subtotal = sc.nextDouble(); 
                
               
    // calculate the results 
                   double discountPercent = 0.0;
                   if (subtotal >= 100)
                      discountPercent = .1;
                   else
                      discountPercent = 0.0;
                   double discountAmount = subtotal * discountPercent;
                   double totalBeforeTax = subtotal – discountAmount;
                   double salesTax = totalBeforeTax * .05;
                   double total = totalBeforeTax + salesTax;
                
                // format and display the results
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                NumberFormat percent = NumberFormat.getPercentInstance();
                String message =
                    "Discount percent: " + percent.format(discountPercent) + "\n" 
                  + "Discount amount: " + currency.format(discountAmount) + "\n"
                  + "Total before tax: " + currency.format(totalBeforeTax) + "\n"
                  + "Sales tax: " + currency.format(salesTax) + "\n"
                  + "Invoice total: " + currency.format(total) + "\n";
                System.out.println(message);
                // see if the user wants to continue
                System.out.print("Continue? (y/n): ");
                choice = sc.next(); 
                System.out.println();
            }
        }
    }

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

    Iron Speed






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