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

Spell out a numeric value

Problem:

You want to spell out a localized number like "thirty-six."

Solution:

ICU4J provides some number formatting capabilities that core Java does not.  One of those features is the ability to spell out a numeric value. 

 

This uses the RuleBasedNumberFormat class to convert a numeric value to a spelled out value.  In other words "75" can be converted to "seventy-five."  This can also be combined with MessageFormat.

 

To spell out a numeric value:

//Get a RuleBasedNumberFormat appropriate for French spellout

RuleBasedNumberFormat rbnf = new RuleBasedNumberFormat(ULocale.FRENCH,RuleBasedNumberFormat.SPELLOUT);

//Call format passing in the numeric value to be formatted

System.out.println(rbnf.format(345));

 

 


 The output:

trois cents quarante-cinq

‹ Parse a spelled-out number up Use a non-default currency ›
  • icu4j
  • number format
  • spellout
  • 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