# Thread: Turning numbers into words

1. Member Join Date
Mar 2011
Posts
93
Rep Power
0

## Turning numbers into words

Hello. Sorry in advance for the stupid and long java question.

I want to write a java program that will turn any number of up to 9 digits in length into the correct englsh words for that number. So if the user inputs 1234, the program will respond with: one thousand two hundred and thirty four. Or if the user inputs 7231560, the program responds with: seven million, two hundred and thrity one thousand, five hundred and sixty. The caveats: the number can be of any length from 1 to 999999999, and doesn't include any commas or other delimiters.

I have successfully written a program that will do this for any number of three digits in length. I then re-wrote the program (posted below) to write a special method to do this conversion of any three digits into the proper words. I was thinking that once I had this special method, it should be easy for me to apply the three digit method to larger numbers. BUT, to apply the three digit method to a larger number, I have to split up the larger number into groups of 3, but in reverse order (so 7231560 becomes 560, 231, and 7, and not 723, 156, and zero).

SO - my question: how do I split up a number of unknown length into groups of 3 from back to front? Also - my approach of applying my three digit method to a larger number may not be the best approach to accomplish the overall task of turning a number into words. Is there a better way to do this?

Here, the program I wrote that turns any three digit number into the proper english words.

Java Code:
```public class SayNumber {

public static String Say3(String s) {
String onePlace = "";
String tenPlace = "";
String hundredPlace = "";
String wordOut = "";
int[] tArray = new int[s.length()];
for (int i = 0; i < s.length(); i++)
tArray[i] = Character.digit(s.charAt(i), 10);

String[] ones = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
String[] teens = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
String[] tens = { null, "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

if (s.length() == 1){
onePlace = (ones[tArray]);
wordOut = onePlace;
}
else if ( (s.length() == 2) && (tArray == 0)) {
tenPlace = (tens[tArray]);
wordOut = tenPlace;
}
else if ((s.length() == 2) && (tArray == 1)) {
tenPlace = (teens[tArray]);
wordOut = tenPlace;
}
else if ( (s.length() == 2) && (tArray != 0) ) {
tenPlace = (tens[tArray]);
onePlace = (ones[tArray]);
wordOut = (tenPlace + "-" + onePlace);
}
else if ((s.length() == 3) && (tArray == 0) && tArray == 0) {
hundredPlace = (ones[tArray]);
wordOut = (hundredPlace + " hundred");
}
else if ((s.length() == 3) && (tArray > 1) && (tArray == 0)) {
hundredPlace = (ones[tArray]);
tenPlace = (tens[tArray]);
wordOut = (hundredPlace + " hundred and " + tenPlace);
}
else if ((s.length() == 3) && (tArray == 0)) {
hundredPlace = (ones[tArray]);
onePlace = (ones[tArray]);
wordOut = (hundredPlace + " hundred and " + onePlace);
}
else if ((s.length() == 3) && (tArray == 1)) {
hundredPlace = (ones[tArray]);
tenPlace = (teens[tArray]);
wordOut = (hundredPlace + " hundred and " + tenPlace);
}
else if ((s.length() == 3) && (tArray > 1)) {
hundredPlace = (ones[tArray]);
tenPlace = (tens[tArray]);
onePlace = (ones[tArray]);
wordOut = (hundredPlace + " hundred and " + tenPlace + "-" + onePlace);
}
return wordOut;
}

public static void main(String[] args) {
String target = args;

if (target.length() < 4)
System.out.print("The number is: " + Say3(target));
else
System.out.print("You Fool!!");
}
}```  Reply With Quote

2. ## Re: Turning numbers into words

Take a look at the String API for useful methods that can give you things like a String's length, a substring of a String, etc.  Reply With Quote

3. Member Join Date
Mar 2011
Posts
93
Rep Power
0

## Re: Turning numbers into words

Thank you Kevin for your useful suggestion. I will take a look.

I think I might have solved the problem in a different fashion. It just occurred to me that if I have a number n, I can do n%1000 to give me the last three digits in their right order, no matter what the original length of n. Then (n/1000)%1000 will give me the next three digits again in proper order. I'm going to go play around with my program some more and see if I can make it work.

Thanks again.  Reply With Quote

4. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Turning numbers into words

Actually, n%1000 will give you any number between 0 and 999 depending on the value of n. So 10% of the possible remainders will have less than three digits.

Regards,
Jim  Reply With Quote

5. ## Re: Turning numbers into words

Suppose you have a number bmmmtttuuu; if b isn't zero you pronounce it as b billiard etc. if mmm isn't zero you pronounce further as mmm million etc. the same goes for ttt thousand etc. all you have to do is write a method that can pronounce a number up to 999.

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•