You want to convert a spelled out number to a Number.
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();
}