Previous | Next | Trail Map | Writing Global Programs | Locale-Sensitive Data


How to Format Numbers

Now that you understand in general terms how formatters work, let's look at some specific examples from the AroundTheWorld applet. Let's start with formatting numbers.

Here's the code that AroundTheWorld uses to format it's three numbers: the GDP, the population, and the literacy rate for the three locales:

gdpFormatter = NumberFormat.getCurrencyInstance(currentLocale);
gdp = (Double) numbers.getObject("GDP");
gdpValue.setText(gdpFormatter.format(gdp));
. . .
populationFormatter = NumberFormat.getNumberInstance(currentLocale);
population = (Integer) numbers.getObject("Population");
populationValue.setText(populationFormatter.format(population));
. . .
literacyFormatter = NumberFormat.getPercentInstance(currentLocale);
literacy = (Double) numbers.getObject("Literacy");
literacyValue.setText(literacyFormatter.format(literacy));
For each number, the code gets a formatter using one of the factory methods provided by NumberFormat. Depending on the data to be formatted, the code gets a currency formatter, a percent formatter, or a formatter for general-purpose decimal numbers. Next, the code gets the data to be formatted from the numbers resource bundle. And finally, the code uses the formatter's format method to format the data into a string which is then used to set the text of the label in the panel.

The NumberFormat.getCurrencyInstance method creates a DecimalFormat object that is set up to format numbers in a way that represents money data in the formatter's Locale. The Locale is specified as a parameter to the factory method. Similary, NumberFormat.getPercentInstance creates a DecimalFormat object that is set up to format numbers that represent percentages. NumberFormat.getNumberInstance creates a DecimalFormat object that formats regular decimal numbers.

This small example illustrates how most programmers will use the number formatters: Most programmers will use one of the already-initialized formatters provided by the JDK. The initialization of the number formatters is hidden and consequently, the full capability of the NumberFormat class is unapparent from this example. Here's a demo program from Taligent that you can use to see what NumberFormat can do.

Since you can't run the applet, here's a picture of it:

Both the 1.1 Number text box and the 1.0 Number text box shows the result of using NumberFormat to format the data entered in either box. The Pattern is used to format the number. You can control the pattern using the radio buttons, pulldown menus, and data entry in the lower portion of the demo program window.

Here's a guide from Taligent that points out interesting things you can do with the demo program. Experiment with the various settings in the demo program according to Taligent's instructions.

The following is the Java code that yields the same results as the demo applet when you first bring it up:

NumberFormat formatter = NumberFormat.getInstance(Locale.US);
String result = formatter.format(-1234.56);
System.out.println(result);
Tweaking the settings in the demo applet corresponds to changing this basic Java code in various ways. This table shows the relationship between the demo programs user interface elements and the number formatter's API.

GUI Element Java Code Equivalent Class
getInstance, or getNumberInstance NumberFormat
getCurrencyInstance NumberFormat
getPercentInstance NumberFormat
getXxxxxInstance(Locale) NumberFormat
from top to bottom, left to right:
setMinimumIntegerDigits, setMaximumIntegerDigits
setMinimumFractionDigits, setMaximumFractionDigits
NumberFormat
from top to bottom, left to right:
setPositivePrefix, setPositiveSuffix
setNegativePrefix, setNegativeSuffix
DecimalFormat
applyPattern DecimalFormat
applyLocalizedPattern DecimalFormat

[PENDING: do an example in the demo and relate it to Java code]


This page incorporates material or code copyrighted by Taligent, Inc. For more information on international resources, see their International Fact Sheet.


Previous | Next | Trail Map | Writing Global Programs | Locale-Sensitive Data