Java Reference
In-Depth Information
der "un menu fantastique." A message that took adjectives and
nouns from lists and displayed them in such a phrase could use a
MessageFormat
object to localize the order.
•
NumberFormat
is an abstract class that defines a general way to
format and parse various kinds of numbers for different locales.
It has two subclasses:
ChoiceFormat
to choose among alternatives
based on number (such as picking between a singular or plural
variant of a word); and
DecimalFormat
to format and parse decimal
numbers. (The formatting capabilities of
NumberFormat
are more
powerful than those provided by
java.util.Formatter
.)
NumberFormat
in turn has four different kinds of "get instance" methods.
Each method uses either a provided
Locale
object or the default locale.
•
getNumberInstance
returns a general number formatter/parser.
This is the kind of object returned by the generic
getInstance
method.
•
getIntegerInstance
returns a number formatter/parser that rounds
floating-point values to the nearest integer.
•
getCurrencyInstance
returns a formatter/parser for currency val-
ues. The
Currency
object used by a
NumberFormatter
can also be re-
trieved with the
getCurrency
method.
•
getPercentInstance
returns a formatter/parser for percentages.
Here is a method you can use to print a number using the format for
several different locales:
public void reformat(double num, String[] locales) {
for (String loc : locales) {
Locale pl = parseLocale(loc);
NumberFormat fmt = NumberFormat.getInstance(pl);
System.out.print(fmt.format(num));
System.out.println("\t" + pl.getDisplayName());
}