The i18n Cookbook - recipies for a global society

  • java cookbook
  • about the author
Home › Java Internationalization Cookbook › Numerical Systems

Java Cookbook

  • Java Internationalization Cookbook
    • Locales
    • Dates and Times
    • Numerical Systems
      • Format and parse an integer
      • Format and parse a decimal
      • Format and parse a percent
      • Format and parse a currency
      • Format Ordinal Numbers
      • Parse a spelled-out number
      • Spell out a numeric value
      • Use a non-default currency
    • Misc
    • Resource Bundles
    • Unicode, Transliteration, and Charactersets

Use a non-default currency

 

Problem:

You want to use a currency other than the default for the region.

Solution:

We can specify a currency separate from a region.  This will give us a numeric format that is appropriate to the locale, but use a currency code for the specified locale.

//Get a locale.
Locale italian = new Locale("it","IT");
//Get a currency instance
Currency c = Currency.getInstance("JPY");
//Get a currency instance
NumberFormat nf = NumberFormat.getCurrencyInstance(italian);
nf.setCurrency(c);
//format and output.  Notice the rounding is not limited to two place on the output.  Rounding
//is now governed by the locale.  Also notice we now use the ISO code istead of a symbol
String f = nf.format(123456.789);
System.out.println(f);

//Parse the value.  We must handle the potential ParseException
//Notice that we can not handle the '¥' mark in the parse
try {
    System.out.println(nf.parse("JPY 123.456,79"));
} catch (ParseException e) {
    e.printStackTrace();
}


 

The output:

JPY 123.456,79
123456.79

‹ Spell out a numeric value up Misc ›
  • currency
  • Printer-friendly version
  • Add new comment

If you are testing any of these recipes in Eclipse and the characters are not displaying correctly in your console visit http://i18ncookbook.com/eclipse_settings.

This site is ad supported.  I hope you find something among our sponsors worth clicking. ;)

i18n search

Google
Custom Search

Search

Tags in Tags

calendar date icu4j Java Locale number format numberformat parse spellout timezone transliteration transliterator
more tags

User login

  • Create new account
  • Request new password
  • java cookbook
  • about the author