views:

398

answers:

2

I'm looking for a java library that would be able to format numbers as words for specified locale, (e.g. 17 = seventeen, 1023 = one thousand twenty three for en_US).

I need this to work with monetary amounts, so decimal fractions need to be supported. The only thing I found on Google was ICU project, particularly this class. It looks somewhat iffy (I haven't actually tried it yet, judging solely by the doc). Did anybody use it? Is there something better out there?

A: 

Why not write your own?

As far as programming projects go, this is not really all that hard.

And as for localizing, don't worry so much. Most languages use more or less the same sort of system to express numbers as words. The only things you are likely to have trouble with are minor differences such as:

  • Word order: Is 24 "twenty-four" or "four-and-twenty"? Easy enough to switch around one line of code to handle both of these.
  • Irregular numbers, such as 11 through 19 in English, 11 through 29 in Spanish, and so on. You might be able to use a lookup table to handle these exceptions, and a regular method for the rest.
  • Millions and billions: some languages use millions and billions like English, but some instead use groupings of 10,000 or 100,000 or some other number. I would handle these using another lookup table.

If you can do this in English, it will not be hard to change the code to pretty much any language.

Robert L
Thanks for your answer, but "why not write your own"? Really? For the same reason I'm not writing connection pools / IoC containers / ORM libraries -someone may have already done that and tested it; and I have better things to do with my time. Thus the question was "does someone know of a library" and not "how would I go about writing this"?
ChssPly76
Quicker to make one than try to find one.
Robert L
I strongly disagree with cowboy approach to software engineering. I've down voted your answer based on that comment
ChssPly76
"Cowboy" approach?If you've seen the kind of mistakes this "cowboy" has seen in "professional" software -- not to mention in "professional" work very far removed from software engineering -- you would see I am not being so unreasonable.
Robert L
LOL! "Professional software is bad but writing my own is good..." Oh boy. And making it localised - well, easy as cake. And well-tested for edge conditions - leave that for the pros, us real programmers _know_ our software works. :-)
Steve McLeod
Edge conditions: have you fed a dollars-and-cents money formatter something like 1.998 ? I have.
Robert L
I note that support for i18n was also one of the requirements. While writing your own converter for a single Locale might be reasonable, much better to somehow crowdsource the code for multiple Locales, I would have thought.
Bill Michell
... and is that not more or less what I suggested?
Robert L
A: 

I imagine digging around the source code of http://www.jonelo.de/java/nc/index.html might turn up something that does what you are after. It must have similar functionality in it to do what it does.

Phantomwhale
Interesting, thank you. However, it's an application rather than a library and it's released under GPL which is definitely a problem in my case. I can't even look at its source now :-( because of that.
ChssPly76