Skip to content

Instantly share code, notes, and snippets.

@chaudharisuresh997
Last active May 4, 2020 14:17
Show Gist options
  • Save chaudharisuresh997/1b14fd8516d58903f720ba351e06c213 to your computer and use it in GitHub Desktop.
Save chaudharisuresh997/1b14fd8516d58903f720ba351e06c213 to your computer and use it in GitHub Desktop.
/*
take 1 2 3 4------1thousand two hundread thitry four
limit 10 lakh
*/
public class NumberToWord{
public String numLetters(char ch){
if(ch=='1'){
return "one";
}else if(ch=='2'){
return "two";
}
else if(ch=='3'){
return "three";
}else if(ch=='4'){
return "four";
}else if(ch=='5'){
return "five";
}else if(ch=='6'){
return "six";
}else if(ch=='7'){
return "seven";
}else if(ch=='8'){
return "eight";
}else if(ch=='9'){
return "nine";
}
return "";
}
public String DigitToWord(String n){
String prefix="";
String suffix="";
if(n.length()==2){
if(n.charAt(0)=='0' && (n.charAt(1)>'0' && n.charAt(1)<'9')){
prefix="";
suffix=numLetters(n.charAt(1));
}
else if(n.charAt(0)=='1'){
prefix=numLetters(n.charAt(1));
suffix="teen";
}else if(n.charAt(0)=='2'){
prefix="twenty";
suffix=numLetters(n.charAt(1));
}
else if(n.charAt(0)=='3'){
prefix="thirty";
suffix=numLetters(n.charAt(1));
}else if(n.charAt(0)=='4'){
prefix="fourty";
suffix=numLetters(n.charAt(1));
}else if(n.charAt(0)=='5'){
prefix="fifty";
suffix=numLetters(n.charAt(1));
}else if(n.charAt(0)=='6'){
prefix="sixty";
suffix=numLetters(n.charAt(1));
}else if(n.charAt(0)=='7'){
prefix="seventy";
suffix=numLetters(n.charAt(1));
}else if(n.charAt(0)=='8'){
prefix="eighty";
suffix=numLetters(n.charAt(1));
}
else if(n.charAt(0)=='9'){
prefix="ninety";
suffix=numLetters(n.charAt(1));
}
}else if(n.length()==1){
suffix=numLetters(n.charAt(0));
}
return prefix+" "+suffix;
}
//1 2 3
public String getNumber(String n){
return DigitToWord(n);
}
public String getWord(int i){
if(i==6 || i==5){
return "lakh ";
}
else if(i==3 || i==4){
return " thousand ";
}else if(i==2){
return "hundread ";
}
return " ";
}
public String extractTwo(int i,String finalStr){
String formNum="";
if(finalStr.charAt(i)=='0' && finalStr.charAt(i-1)=='0'){}else{
String numChar=""+finalStr.charAt(i)+""+finalStr.charAt(i-1);
formNum=""+getNumber(numChar);
formNum+=""+getWord(i);
}
return formNum;
}
public String extractOne(int i,String finalStr){
String formNum="";
String numChar=""+finalStr.charAt(i);
formNum=""+getNumber(numChar);
formNum+=" "+getWord(i);
return formNum;
}
public String process(int number){
String numStr=""+number;
StringBuffer sb=new StringBuffer(numStr);
String finalStr=sb.reverse().toString();
sb=new StringBuffer();
int i=finalStr.length()-1;
while(i>=0){
if(i==0){
sb.append(""+extractOne(i,finalStr));
i--;
}else if(i==1){
sb.append(""+extractTwo(i,finalStr));
i-=2;
}else if(i==2){
sb.append(""+extractOne(i,finalStr));
i--;
}else if(i==3){
sb.append(""+extractOne(i,finalStr));
i--;
}else if(i==4){
sb.append(""+extractTwo(i,finalStr));
i-=2;
}else if(i==5){
sb.append(""+extractOne(i,finalStr));
i--;
}else if(i==6){
sb.append(""+extractTwo(i,finalStr));
i-=2;
}
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(new NumberToWord().process(1103));
System.out.println(new NumberToWord().process(123456));
System.out.println(new NumberToWord().process(103304)); //1lakh 3thousand 3hundread 4
System.out.println(new NumberToWord().process(23));
System.out.println(new NumberToWord().process(3));
/*onethousand onehundread
onelakh twenty threethousand fourhundread fifty six
onelakh thousand threehundread
twenty three
three
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment