Spell out numbers using icu4j

December 9, 2010 by Jean

Needed to spell out numbers (421 → four hundred twenty-one) for printing on receipts. Ended using icu4j, a Java internalization library developed by IBM.

Once the icu4j.jar is available to your ColdFusion class path, something like this should work:

function spellOut(int)
{
locale = createObject("java", "com.ibm.icu.util.ULocale").init('en_US');
ruleBased = createObject("java","com.ibm.icu.text.RuleBasedNumberFormat").init(locale,1); return ruleBased.format(javacast("long", arguments.int),"%spellout-numbering"); }

Spell out formats:

The list of available formats (%spellout-numbering above) depends on the locale:

locale = createObject("java", "com.ibm.icu.util.ULocale").init('fr_FR');
ruleBased = createObject("java", "com.ibm.icu.text.RuleBasedNumberFormat").init(locale,1);
names=ruleBased.getRuleSetNames();
for (j=1; j lte arrayLen(names); j=j+1) { writeOutput("
#names[j]#: #ruleBased.format(123,names[j])#
");
}

References:

In: ColdFusion