Created
April 24, 2012 15:21
-
-
Save stevesun21/2480559 to your computer and use it in GitHub Desktop.
Java implemented number wording translator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NumberWordingTranslator { | |
private final static String[] hundredWords={ | |
"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", | |
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", | |
"Twenty", null, null, null, null, null, null, null, null, null, | |
"Thirty", null, null, null, null, null, null, null, null, null, | |
"Forty", null, null, null, null, null, null, null, null, null, | |
"Fifty", null, null, null, null, null, null, null, null, null, | |
"Sixty", null, null, null, null, null, null, null, null, null, | |
"Seventy", null, null, null, null, null, null, null, null, null, | |
"Eighty", null, null, null, null, null, null, null, null, null, | |
"Ninety", null, null, null, null, null, null, null, null, null, | |
"Hundred" | |
}; | |
private final static int[] places={1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000}; | |
public static String translateToDigitWords(int number) { | |
StringBuffer str=new StringBuffer(); | |
int numTemp=number; | |
int placeIndex=places.length-1; | |
while(numTemp>0) | |
{ | |
int mod=numTemp%places[placeIndex]; | |
int digit=(numTemp-mod)/places[placeIndex]; | |
placeIndex--; | |
if(str.length()==0 && digit==0) continue; | |
str.append(hundredWords[digit]); | |
str.append(" "); | |
numTemp=numTemp-(numTemp-mod); | |
} | |
if(str.length()>0) | |
str.deleteCharAt(str.length()-1); | |
return str.toString(); | |
} | |
public static String translateToNumberWords(int number) { | |
StringBuffer str=new StringBuffer(); | |
if(number>=1000000 && number<1000000000) | |
{ | |
int mod=number%1000000; | |
int restNumber=(number-mod)/1000000; | |
str.append(translateToNumberWords(restNumber)); | |
str.append(" Million "); | |
str.append(translateToNumberWords(mod)); | |
return str.toString(); | |
} | |
if(number>=1000 && number<1000000) | |
{ | |
int mod=number%1000; | |
int restNumber=(number-mod)/1000; | |
str.append(translateToNumberWords(restNumber)); | |
str.append(" Thousand "); | |
str.append(translateToNumberWords(mod)); | |
return str.toString(); | |
} | |
if(number>=100 && number<1000) | |
{ | |
int mod=number%100; | |
int restNumber=(number-mod)/100; | |
str.append(translateToNumberWords(restNumber)); | |
str.append(" Hundred "); | |
str.append(translateToNumberWords(mod)); | |
return str.toString(); | |
} | |
if(number>=20 && number<100) | |
{ | |
int mod=number%10; | |
int restNumber=number-mod; | |
str.append(hundredWords[restNumber]); | |
str.append(" "); | |
str.append(translateToNumberWords(mod)); | |
return str.toString(); | |
} | |
if(number>0 && number<20) | |
{ | |
str.append(hundredWords[number]); | |
return str.toString(); | |
} | |
return str.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment