Number and Currency Formatting |
By invoking the methods provided by the NumberFormatclass, you can format numbers, currencies, and percentages according toLocale
. However, there's a catch:NumberFormat
may not support theLocale
you specify. To find out whichLocale
definitions are supported byNumberFormat
, invoke thegetAvailableLocales
method:IfLocale[] locales = NumberFormat.getAvailableLocales();NumberFormat
does not support aLocale
that you need, you can create your own formats. We'll explain this procedure in the next section, Formatting with Patterns.The material that follows shows you how to get locale-specific formats for numbers, currencies, and percentages. The code examples in this material are from a sample program called NumberFormatDemo.java.
Numbers
You can use theNumberFormat
factory methods to format primitive type numbers, such asdouble
, and their corresponding wrapper objects, likeDouble
.In the following code example, we're going to format a
Double
according toLocale
. First, we get a locale-specific instance ofNumberFormat
by callinggetNumberInstance
. Then we invoke theformat
method with theDouble
as an argument. Theformat
method returns the formatted number in aString
, which is ready to be displayed.The output from this example shows how the format of the same number varies withDouble amount = new Double(345987.246); NumberFormat numberFormatter; String amountOut; numberFormatter = NumberFormat.getNumberInstance(currentLocale); amountOut = numberFormatter.format(amount); System.out.println(amountOut + " " + currentLocale.toString());Locale
:345 987,246 fr_FR 345.987,246 de_DE 345,987.246 en_USCurrencies
If you're writing business applications, chances are you'll need to format and display currencies. You format currencies in the same manner as numbers, except with currencies you callgetCurrencyInstance
to create a formatter. When you invoke theformat
method, it returns aString
that includes the formatted number and the appropriate currency sign.The next code example shows how to format currency in a locale-specific manner:
The output generated by the preceeding lines of code is as follows:Double currency = new Double(9876543.21); NumberFormat currencyFormatter; String currencyOut; currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale); currencyOut = currencyFormatter.format(currency); System.out.println(currencyOut + " " + currentLocale.toString());At first glance this output may look wrong to you, because the numeric values are all the same. Of course, 9 876 543,21 F is not equivalent to 9.876.543,21 DM. However, bear in mind that the9 876 543,21 F fr_FR 9.876.543,21 DM de_DE $9,876,543.21 en_USNumberFormat
class is unaware of exchange rates. The methods belonging to theNumberFormat
class format currencies, but do not convert them.Percentages
You can also use the methods of theNumberFormat
class to format percentages. To get the locale-specific formatter, invoke thegetPercentInstance
method. With this formatter, a fraction like 0.75 is displayed as 75%.The following code sample shows how to format a percentage.
Double percent = new Double(0.75); NumberFormat percentFormatter; String percentOut; percentFormatter = NumberFormat.getPercentInstance(currentLocale); percentOut = percentFormatter.format(percent);
Number and Currency Formatting |