Java Reference
In-Depth Information
Locale.getDefault().getDisplayName());
NumberFormat nf = NumberFormat.getCurrencyInstance();
String formattedCurrency = nf.format(value);
System.out.printf("%s\n", formattedCurrency);
Currency c = Currency.getInstance(Locale.US);
nf.setCurrency(c);
formattedCurrency = nf.format(value);
System.out.printf("%s\n\n", formattedCurrency);
The previous code prints out the following:
Default locale:
12,345
USD12,345
How It Works
You use a NumberFormat instance to format currency values. You should explicitly
call the getCurrencyInstance() method to create a formatter for currencies:
NumberFormat nf = NumberFormat.getCurrencyInstance();
The previous formatter will use your default locale's preferences for formatting
numbers as currency values. Also, it will use a currency symbol that is associated with
the locale's region. However, one very common use case involves formatting a value
for a different region's currency.
Use the setCurrency() method to explicitly set the currency in the number
formatter:
nf.setCurrency(aCurrencyInstance); // requires a Currency
instance
Note that the java.util.Currency class is a factory. It allows you to create
currency objects in two ways:
Currency.getInstance(Locale locale)
Currency.getInstance(String currencyCode)
Search WWH ::




Custom Search