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

Format and parse an integer

Problem:

You want to format and parse integer values.

Solution:

The Java NumberFormat class makes formatting and parsing a number is localized manner easy and efficient.  Simple specify the locale when retrieving the NumberFormat instance.

 

To format and parse a number in Hindi:

//Get a locale.  Hindi in India here.
ULocale hindi = new ULocale("hi_IN");
//Get an instance of an integer format.
NumberFormat nf = NumberFormat.getIntegerInstance(hindi);
//format and output.  The decimal is cut off and rounded.
String f = nf.format(1234234234.556);
System.out.println(f);

//Parse the value.  We must handle the potential ParseException
try {
    System.out.println(nf.parse("१,२३,४२,३४,२३५"));
} catch (ParseException e) {
    e.printStackTrace();
}

 


The output:

१,२३,४२,३४,२३५
1234234235

‹ Numerical Systems up Format and parse a decimal ›
  • numberformat
  • number format
  • parse
  • 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