The Java.text package allows you to format text messages, dates, and numeric values in a way that is not relevant to a particular language. Many people use resource bundles with the Messageformat class to localize messages for users. More people seem to use the DateFormat and SimpleDateFormat classes to manipulate date strings, both for input and for output. The most uncommon usage seems to be to use the NumberFormat class and its associated subclasses DecimalFormat and Choiceformat. In this month's discussion, we'll look at these three underutilized classes and Currency classes to see how globalized j2se 1.4 has become.
Numeric formatting base class: NumberFormat
If you are from the United States, you place commas in the middle of a larger number to represent thousands and millions (and so on, using a comma for every three values). For floating-point numbers, you place a decimal point between the integer part and the fractional part. For money, the currency symbol $ is placed in front of the amount. If you have never been to a place outside the United States, you may not care about the Japanese currency formatted with Yuan (¥), the British currency formatted with Sterling (£), or the currencies of other European countries expressed in euros (€).
For those currencies that we really care about, we can use NumberFormat and their associated classes to format them. Developers use the NumberFormat class to read the values entered by the user and format the output that will be displayed to the user.
Like DateFormat, NumberFormat is an abstract class. You never create an instance of it-instead, you always use its subclasses. Although subclasses can be created directly through the constructors of subclasses, the NumberFormat class provides a series of Get Xxxinstance () methods for obtaining specific locale versions of different types of numeric classes. There are five such methods:
Getcurrencyinstance ()
GetInstance ()
Getintegerinstance ()
Getnumberinstance ()
Getpercentinstance ()
Which method you use depends on the type of value you want to display (or the type of input you want to accept). Each method provides two versions-one version applies to the current locale, and the other version accepts a locale as a parameter to possibly designate a different region.
In J2SE 1.4, numberformat new content is the Getintegerinstance (), getcurrency (), and Setcurrency () methods. Now let's look at the new Getintegerinstance () method. The get/set monetary approach will be explored later.
The basic process of using NumberFormat is to get an instance and use that instance. It really takes some thinking to pick the right example. Usually you don't want to use the generic getinstance or Getnumberinstance () version, because you don't know exactly what you're going to get. Instead, you'll use a method like Getintegerinstance (), because you want to show something as an integer without needing any decimal value. Listing 1 shows this, where we show the value 54321 as a suitable format for the United States and Germany.
Listing 1. Using NumberFormat
import java.text.*;
import java.util.*;
public class IntegerSample {
public static void main(String args[]) {
int amount = 54321;
NumberFormat usFormat =
NumberFormat.getIntegerInstance(Locale.US);
System.out.println(usFormat.format(amount));
NumberFormat germanFormat =
NumberFormat.getIntegerInstance(Locale.GERMANY);
System.out.println(germanFormat.format(amount));
}
}
Running the code produces the output shown in Listing 2. Note the comma delimiter in the first format (United States) and the dot separator in the second format.
Listing 2. NumberFormat output
54,321
54.321