The i18n Cookbook - recipies for a global society

  • java cookbook
  • about the author
Home › Java Internationalization Cookbook › Resource Bundles

Java Cookbook

  • Java Internationalization Cookbook
    • Locales
    • Dates and Times
    • Numerical Systems
    • Misc
    • Resource Bundles
      • Handle plural text in a localizable manner.
      • How to combine dynamic and static data
      • Load properties from outside the classpath
      • Spell out the numeric value in a concatenated string
    • Unicode, Transliteration, and Charactersets

Spell out the numeric value in a concatenated string

 Problem:

You want to spell out a dynamic numeric value being combined with a static resource.

Solution:

Java MessageFormat provides a number of options for formatting the values passed in.  ICU4J provides even more options.

One of the more interesting formatting options is the spellout format.  Spell out will actually write out a numeric value, such as thirty-five vs. 35.

ICU4J's MessageFormat is used similar to the code Java class.

To format a pattern using a spellout pattern:

//Create an Array of Objects
Object[] tmp = {345};
//Create our pattern.  We specify that we will replace using the first element in the array
//and use a spellout pattern
String msg = "I have {0,spellout} coffee cups in my car.";
//Retrieve the MessageFormat
MessageFormat mf = new MessageFormat(msg,ULocale.ENGLISH);
//Call format passing in the array
System.out.println(mf.format(tmp));
//Now lets do it in Japanese
String jMsg = "私の車の中に{0,spellout}個のコーヒーカップがあります。";
MessageFormat mf2 = new MessageFormat(jMsg,ULocale.JAPANESE);
System.out.println(mf2.format(tmp));


The output is:

I have three hundred and forty-five coffee cups in my car.

私の車の中に三百四十五個のコーヒーカップがあります。

 

 

‹ Load properties from outside the classpath up Unicode, Transliteration, and Charactersets ›
  • icu4j
  • messageformat
  • 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