The number is ranging from 1 to 999999999.
public class ConvertIntegertoEnglish {
private String getCountSuffix(int value){
String word = "";
switch(value){
case 3:
word = "Hundred "; return word;
case 4:
word = "Thousand "; return word;
case 5:
word = "Thousand "; return word;
case 6:
word = "Lac "; return word;
case 7:
word = "Lacs "; return word;
case 8:
word = "Crore "; return word;
case 9:
word = "Crores "; return word;
default:
return "";
}
}
private String getCountPrefix(String count){
String word = "";
int value = Integer.valueOf(count);
if(value >=20 && value <=29){
word = "Twenty ";
}else if(value >=30 && value <=39){
word = "Thirty ";
}else if(value >=40 && value <=49){
word = "Fourty ";
}else if(value >=50 && value <=59){
word = "Fifty ";
}else if(value >=60 && value <=69){
word = "Sixty ";
}else if(value >=70 && value <=79){
word = "Seventy ";
}else if(value >=80 && value <=89){
word = "Eighty ";
}else if(value >=90 && value <=99){
word = "Ninty ";
}else if(value == 0){
word = "";
}else if(value == 1){
word = "One ";
}else if(value == 2){
word = "Two ";
}else if(value == 3){
word = "Three ";
}else if(value == 4){
word = "Four ";
}else if(value == 5){
word = "Five ";
}else if(value == 6){
word = "Six ";
}else if(value == 7){
word = "Seven ";
}else if(value == 8){
word = "Eight ";
}else if(value == 9){
word = "Nine ";
}else if(value == 10){
word = "Ten ";
}else if(value == 11){
word = "Eleven ";
}else if(value == 12){
word = "Tweleve ";
}else if(value == 13){
word = "Thirteen ";
}else if(value == 14){
word = "Fourteen ";
}else if(value == 15){
word = "Fifteen ";
}else if(value == 16){
word = "Sixteen ";
}else if(value == 17){
word = "Seveneteen ";
}else if(value == 18){
word = "Eighteen ";
}else if(value == 19){
word = "Nineteen ";
}
return word;
}
public String convertIntoEnglish(int num){
String number = ""+num;
int len = number.length();
String sentence ="";
String temp ="";
for (int i=len;i>0;i--){
// 00 00 00 000
int n = len - i;
if((i%2)>0 && i>3 ){
temp = number.substring(n,(n + 2));
int v = Integer.parseInt(temp);
if(v < 10) continue;
sentence = sentence + getCountPrefix(temp);
if(v > 19){
temp = temp.substring(1);
sentence = sentence + getCountPrefix(temp);
}
sentence = sentence + getCountSuffix(i);
i--;
}else if((i%2)==0 && i>3){
temp = number.substring(n,(n + 1));
sentence = sentence + getCountPrefix(temp);
sentence = sentence + getCountSuffix(i);
}else if(i == 3){
temp = number.substring(n,(n + 1));
if(temp.startsWith("0")) continue;
sentence = sentence + getCountPrefix(temp);
sentence = sentence + getCountSuffix(3);
}else{
if(i == 2)
temp = number.substring(len-2);
else
temp = number.substring(len-1);
int v = Integer.parseInt(temp);
sentence = sentence + getCountPrefix(temp);
if(v > 19){
temp = temp.substring(1);
sentence = sentence + getCountPrefix(temp);
}
i--;
}
}
return sentence;
}
public static void main(String[] str){
System.out.println(new ConvertIntegertoEnglish().convertIntoEnglish(23451147));
}
}