Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
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.
Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq