Home arrow Java arrow Page 3 - How the BigDecimal Class Helps Java Get its Arithmetic Right

How the BigDecimal Class Helps Java Get its Arithmetic Right

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

Author Info:
By: Murach Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 13
February 02, 2006
  1. · How the BigDecimal Class Helps Java Get its Arithmetic Right
  2. · The math problems in the Invoice application
  3. · How to use the BigDecimal class
  4. · How to use BigDecimal arithmetic in the Invoice application

print this article

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:


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


Figure 3 The constructors and methods for the BigDecimal class

Constructors of the BigDecimal class

Methods of the BigDecimal class

The RoundingMode enumeration


Two of the values in the RoundingMode enumeration



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


blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials