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.
Next: How to use BigDecimal arithmetic in the Invoice application >>
More Java Articles
More By Murach Publishing
|
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.
|
|