converting int (or Integer) to String name e.g. '5' to 'five'

Is there any good way of getting a textual representation of a number, for example can you change the number 5 (represented, lets say, as an int) to the text 'five'.

The only ways I can think of are very long winded (e.g. an array of ints and a corresponding array of string representations).

Many thanks.

[326 byte] By [KungFooHamstera] at [2007-11-27 10:22:07]
# 1

> Is there any good way of getting a textual

> representation of a number, for example can you

> change the number 5 (represented, lets say, as an

> int) to the text 'five'.

> The only ways I can think of are very long winded

> (e.g. an array of ints and a corresponding array of

> string representations).

> Many thanks.

You don't need the array of ints, but you do need the array of Strings. Once you have the Strings set up, it's not that long winded or difficult of a program. In fact it's quite easy and instructional. I did this not too long ago for the numbers 0 to 1,000,000,000,000. You should do it, and then if you like post it here and we could critique it.

Good luck.

petes1234a at 2007-7-28 17:14:03 > top of Java-index,Java Essentials,Java Programming...
# 2

And then make sure it supports i18n! ;-)

-Puce

Pucea at 2007-7-28 17:14:03 > top of Java-index,Java Essentials,Java Programming...
# 3

OK, as I wanted to print the numbers 1-99 in decending order, this is what I've done.

public class IntToString

{

public IntToString()

{

}

public static void main(String args[])

{

String [] ones = {"","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"};

String [] teens = {"TEN","ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN"};

String [] tens = {"","TEN","TWENTY","THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"};

//String [] hundreds = {"","hundred","thousand","million","billion","trillion"};

for (int i = 9; i > -1; i--)

{

if (i == 1)

{

for (int k = 9; k > -1; k--)

{

String no3 = teens[k];

System.out.println(no3);

}

}

else

{

String no1 = tens[i];

for (int j = 9; j >-1; j--)

{

String no2 = ones[j];

System.out.println(no1 + " "+ no2);

}

}

}

}

}

KungFooHamstera at 2007-7-28 17:14:03 > top of Java-index,Java Essentials,Java Programming...
# 4

I haven't run your code, but I will say that if you want to implement the larger numbers, hundred is in a separate class of its own and kind of doesn't belong in the array that you have it in. The reason is two fold. First of all, hundred doesn't logically fit into this progression of thousands:

10^3 = thousand

10^6 = million

10^9 = billion

10^12 = trillion

Next, you can have five hundred million or seven hundred billion, so like the ones, teens, and tens, it is potentially re-used in each group of 1000.

petes1234a at 2007-7-28 17:14:03 > top of Java-index,Java Essentials,Java Programming...
# 5

I'd like to see i18n, too. For example 92 in French is quatre-vingt-douze (four twenties and twelve)

BigDaddyLoveHandlesa at 2007-7-28 17:14:03 > top of Java-index,Java Essentials,Java Programming...
# 6

public String intToString(int num)

{

String[] names = new String[]

{

"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", /* ... */ "nine-hundred-ninety-nine thousand nine-hundred-ninety-nine", "one million"

};

return names[num];

}

;-)

CaptainMorgan08a at 2007-7-28 17:14:03 > top of Java-index,Java Essentials,Java Programming...
# 7

> [code]public String intToString(int num)

> {

>String[] names = new String[]

> {

> "zero", "one", "two", "three", "four", "five",

> "six", "seven", "eight", "nine", "ten", /* ... */

> "nine-hundred-ninety-nine thousand

> nine-hundred-ninety-nine", "one million"

>};

>return names[num];

> code]

>

> ;-)

Something tells me you might run into the maximum size of a class writing that one all out. Better split it into seperate files :-)

Aknibbsa at 2007-7-28 17:14:03 > top of Java-index,Java Essentials,Java Programming...
# 8

> OK, as I wanted to print the numbers 1-99 in

> decending order...

Hey, you aren't doing "99 Bottles of Beer," are you? 'Cuz it's been done already:

http://99-bottles-of-beer.net/language-java-1162.html

uncle_alicea at 2007-7-28 17:14:03 > top of Java-index,Java Essentials,Java Programming...
# 9

Uncle Alice,

Yes, we are. But it's 100 bottles of beer in this case, and every 10 bottles has to be empty! It's just a little weekly(ish) teazer we're having at work where we have a programming task that we must complete in a language that we're not overly familiar with.

I'm going to do it in Java and then in Ruby.

Thanks for the URL, but cheating kind of defeats the object ;-)

KungFooHamstera at 2007-7-28 17:14:03 > top of Java-index,Java Essentials,Java Programming...
# 10

> I'd like to see i18n, too.

> For example 92 in French is quatre-vingt-douze (four twenties and twelve)

But in other French dialects (Belgian? Canadian?) it is nonante-deux.

So be careful with that Locale examining!

BIJ001a at 2007-7-28 17:14:03 > top of Java-index,Java Essentials,Java Programming...
# 11

It's actually quite an interesting problem, probably lending itself to a recursive routine.

In English there are separate words up to 20, above that you do the tens and the units separately, then if it's above a hundred you deal with the hundreds first, then the rest.

So you have a recursive routine that handles 1 - 20 without recursion, but above that starts to split off early digits and recurse to do the low order. Above a thousand, you recurse twice with the word "thousand" between and so on.

malcolmmca at 2007-7-28 17:14:03 > top of Java-index,Java Essentials,Java Programming...