views:

339

answers:

6

Is there any existing builtin (or third party) Java class or library that can convert numbers to natural language strings; e.g. 1 -> "one", 2 -> "two", 1256 -> "a one thousand two hundred and fifty-six", etc.?

A: 

... built in? ... no.

vector
OK. Or external library...
Nicholas Bayborodin
.... hm, 'no' was just a quick answer ;-), sorry.Perhaps a text to speech library might have something like that (and that's only if this type of parsing would be a required intermediate step in the process), if you can find it.
vector
Edited the question ... per @Nicholas's comment above.
Stephen C
+2  A: 

There is no built-in method with Java API. You have to create a method for the same. Take a look at Sample

adatapost
That RoseIndia site might be one of the ugliest on the web
Andy White
And the linked program is sooooo wrong. "Hundrad"? What language is that?
Stephen C
Wonder how it handles teenagers..... Oh I get it; it converts them to centenarians. Try System.out.println("words is :=" + num.convert(11));
Vineet Reynolds
@adatapost, you might want to link to a better algo. Unfortunately that one at RoseIndia is broken.
Vineet Reynolds
Thanks @Vineet, I show him/her a way. I don't want to comment on that site.
adatapost
@adatapost, I know you posted the link out of good intentions, but that code does not work for numbers 10-19, 110-119 etc.
Vineet Reynolds
Hundrad is Sweedish I belive, but it sure looks more like broken English.
Thorbjørn Ravn Andersen
+3  A: 

Try googling

java "number to words"

See http://www.rgagnon.com/javadetails/java-0426.html for one good example.

mobrule
A: 

As a sidenote: You may look into java i18n relevant aspects to see how to deal with language specific ressources in java.

http://java.sun.com/docs/books/tutorial/i18n/index.html

Keywords to look for are ResourceBundle and such.

Don't start to hardcode any natural language strings in static arrays or similar. This is almost always wrong.

The algorithm to convert numbers like 1.302.088 to a string is up to yours, I guess.

pimpf0r
A: 

hey why dont you try this http://forums.sun.com/thread.jspa?threadID=625527

+1  A: 

ICU is the right tool for this. Please check out,

http://icu-project.org/apiref/icu4j/com/ibm/icu/text/RuleBasedNumberFormat.html

ZZ Coder