Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatting numbers(spellout) with icu4j

Tags:

java

icu

I want to localize numbers by spelling them out with a locale and ended up using ICU4J. I succeeded in many locales but didn't seem to get it done for a few like Georgian, Turkish or Arabic.

ULocale locale = new ULocale("Tr");  //Turkish
Double d = Double.parseDouble(input);
NumberFormat formatter = new RuleBasedNumberFormat(locale, RuleBasedNumberFormat.SPELLOUT);
String result = formatter.format(d);

When I debug the ruleset inside by formatter object seems empty and it prints out in English(as default I guess).

In ICU website it is written that: ICU provides number spellout rules for several locales, but not for all of the locales that ICU supports, and not all of the predefined rule types. Also, as of release 2.6, some of the provided rules are known to be incomplete.

But it seems ICU's own demo page can do it for that locales too(http://demo.icu-project.org/icu4jweb/numero.jsp)

I also can locate .res files in the icu5j-53.1.jar under my .m2 folder (\icu4j-53.1\com\ibm\icu\impl\data\icudt53b\rbnf)

Would appreciate any help about how the demo page can manage to do it and why I can't?

like image 897
Sinan Kucukkoseler Avatar asked Sep 30 '22 14:09

Sinan Kucukkoseler


1 Answers

I've tested your code with version 53.1 and got correct Turkish output. I then retested with version 3.4.4 and got English output as you described in your question.

Most likely, you are pulling in an older version through a transitive maven dependency.

like image 150
Robby Cornelissen Avatar answered Oct 03 '22 03:10

Robby Cornelissen