# Turning numbers into words

• 03-21-2013, 08:27 PM
Humphrey Bogart
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.

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[0]]);                         wordOut = onePlace;                 }                 else if ( (s.length() == 2) && (tArray[1] == 0)) {                         tenPlace = (tens[tArray[0]]);                         wordOut = tenPlace;                 }                 else if ((s.length() == 2) && (tArray[0] == 1)) {                         tenPlace = (teens[tArray[1]]);                         wordOut = tenPlace;                 }                 else if ( (s.length() == 2) && (tArray[1] != 0) ) {                         tenPlace = (tens[tArray[0]]);                         onePlace = (ones[tArray[1]]);                         wordOut = (tenPlace + "-" + onePlace);                 }                 else if ((s.length() == 3) && (tArray[1] == 0) && tArray[2] == 0) {                         hundredPlace = (ones[tArray[0]]);                         wordOut = (hundredPlace + " hundred");                 }                 else if ((s.length() == 3) && (tArray[1] > 1) && (tArray[2] == 0)) {                         hundredPlace = (ones[tArray[0]]);                         tenPlace = (tens[tArray[1]]);                         wordOut = (hundredPlace + " hundred and " + tenPlace);                 }                 else if ((s.length() == 3) && (tArray[1] == 0)) {                         hundredPlace = (ones[tArray[0]]);                         onePlace = (ones[tArray[2]]);                         wordOut = (hundredPlace + " hundred and " + onePlace);                 }                 else if ((s.length() == 3) && (tArray[1] == 1)) {                         hundredPlace = (ones[tArray[0]]);                         tenPlace = (teens[tArray[2]]);                         wordOut = (hundredPlace + " hundred and " + tenPlace);                 }                 else if ((s.length() == 3) && (tArray[1] > 1)) {                         hundredPlace = (ones[tArray[0]]);                         tenPlace = (tens[tArray[1]]);                         onePlace = (ones[tArray[2]]);                         wordOut = (hundredPlace + " hundred and " + tenPlace + "-" + onePlace);                 }                 return wordOut;         }         public static void main(String[] args) {                 String target = args[0];                                                         if (target.length() < 4)                         System.out.print("The number is: " + Say3(target));                 else                         System.out.print("You Fool!!");         } }```
• 03-21-2013, 08:42 PM
KevinWorkman
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.
• 03-21-2013, 09:08 PM
Humphrey Bogart
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.
• 03-21-2013, 09:51 PM
jim829
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
• 03-23-2013, 10:18 AM
JosAH
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