BigDecimal class and numeric formatting. The first step to teaching you both of those topics is to first create BigDecimal objects. We will use the BigDecimal class in the java.math library to hold values. You can create a BigDecimal object in the following manner:
BigDecimal amount = new BigDecimal("1115.37");
In the above case, the String argument to the BigDecimal constructor determines the value of the object created. The value of "1115.37" might represent, for example, a monthly mortgage payment in dollars, or a checkbook balance. To display the amount, you can
use the BigDecimal class's toString() method:
System.out.println(amount.toString());
A program that creates and displays a BigDecimal amount is shown below:
import java.math.*;
public class Mortgage {
public static void main(String[] args) {
BigDecimal payment = new BigDecimal("1115.37");
System.out.println(payment.toString());
}
}
Output from the above program is:
1115.37
Since we are dealing with money, it would be nice to have the amounts held by BigDecimal objects properly formatted, which for US currency would include a dollar sign and a comma as a thousands separator. (For
other currencies, please see the section Currency of Other Countries below). The NumberFormat class, found in the java.text library, can create an appropriate object for US currency with the following code:
NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
Note that the Locale class, used as an argument for the getCurrencyInstance() method above, is found in the java.util library.
The NumberFormat's format() method, which we will be using next, takes a double or long primitive as an argument, so first we will turn the BigDecimal object into a double using BigDecimal's doubleValue() method:
double doublePayment = payment.doubleValue();
Now we use NumberFormat's format() method to create a String:
String s = n.format(doublePayment);
Putting these steps in a program, we then have:
import java.math.*;
import java.text.*;
import java.util.*;
public class Mortgage2 {
public static void main(String[] args) {
BigDecimal payment = new BigDecimal("1115.37");
NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
double doublePayment = payment.doubleValue();
String s = n.format(doublePayment);
System.out.println(s);
}
}
Output from the above program is:
,115.37
Readers should note that creating a double value involves a small loss in the value's accuracy. While the inaccuracies are too small to be seen in this article's examples,
they are visible in very large amounts. Therefore, you cannot rely upon NumericFormat to produce accurate results with very large numbers (about 13 or more digits).
In the previous example, we used Locale.US as the argument passed to the getCurrencyInstance() method to specify the currency of the country (United States) with which we'd be working. Java is not limited to working
with US currency however. For example, you would use Locale.GERMANY, Locale.FRANCE, or Locale.ITALY to specify the currencies of Germany, France, and Italy, respectively. The topic of internationalization is a subject in
its own right; see the Resources section for a link to more information.
Too many typosBy Anonymous on August 19, 2009, 11:35 amMost significant digit truncated in many places.
Reply | Read entire comment
Helpful - Small typoBy Anonymous on August 7, 2009, 5:56 pmHelpful article - I think "The investor makes an additional 00.00 purchase of shares" should be "The investor makes an additional 200.00 purchase of shares"
Reply | Read entire comment
Nice one. Very helpfulBy Anonymous on June 21, 2009, 1:16 pmNice one. Very helpful
Reply | Read entire comment
Nice oneBy Anonymous on March 8, 2009, 2:53 pmNice one
Reply | Read entire comment
View all comments