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

Parse a spelled-out number

Problem:

You want to convert a spelled out number to a Number.

Solution:

Parsing a formatted number in Java takes a new twist when the number is fully spelled out.   This can be easily accomplished thanks to the ICU4J library from IBM.  

 

To parse a spelled out number:

//Get a RuleBasedNumberFormat appropriate for French spellout

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

//The String to parse

String number = "three hundred and forty-five";

//We need to handle the potential ParseException

try

{

    //Parse the number

    System.out.println(rbnf.parse(number));

}

catch (ParseException e)

{

    e.printStackTrace();

}

 

The output:
345

 

The same thing for a French Locale:

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

String number = "trois cents quarante-cinq";

try

{

    System.out.println(rbnf.parse(number));

}

catch (ParseException e)

{

    // TODO Auto-generated catch block

    e.printStackTrace();

}

 

‹ Format Ordinal Numbers up Spell out a numeric value ›
  • number format
  • parse
  • rulebasednumberformat
  • 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