Results 1 to 5 of 5
Thread: Turning numbers into words
 03212013, 08:27 PM #1Member
 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 rewrote 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[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!!"); } }
 03212013, 08:42 PM #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.
How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 03212013, 09:08 PM #3Member
 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.
 03212013, 09:51 PM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,023
 Rep Power
 6
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03232013, 10:18 AM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,772
 Blog Entries
 7
 Rep Power
 21
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,
Joscenosillicaphobia: the fear for an empty beer glass
Similar Threads

changing telephone numbers into words.
By akeni in forum New To JavaReplies: 20Last Post: 11242011, 01:37 PM 
Converting numbers to words
By Zora in forum New To JavaReplies: 2Last Post: 11142011, 11:10 AM 
turning string numbers into doubles
By imorio in forum New To JavaReplies: 5Last Post: 11102010, 11:25 PM 
Turning numbers into asterisks.
By BugginVT in forum New To JavaReplies: 7Last Post: 02252009, 08:38 AM
Bookmarks