Spelling numbers in Java in Crore, Lac and Thousand

Untitled
/*

1    crore  = 100  lac
1      lac  = 100  thousand
1 thousand  =  10  hundred

*/

public class SpellNumber{
        
        static long num;

        static final int DIGIT_LIMIT=20;

        static String spellNum[]=new String[100];
        static String tys[]=new String[10];

        static String positions[]=new String[DIGIT_LIMIT];
        static String scale[]={"","hundred","thousand1","thousand2", "lac1", "lac2", "crore1", "crore2"};

        static int digits[]=new int[DIGIT_LIMIT];



        static String spellDigits(int twoDgt){

            if(spellNum[twoDgt] != null) return spellNum[twoDgt];

            int firstDigit=twoDgt%10;
            twoDgt=twoDgt/10;
            int secondDigit=twoDgt;

            String out="";
            out=tys[secondDigit]+" ";
            out=out+spellNum[firstDigit];
            return out;
        }

    public static void main(String[] args) {

for(;;){

    System.out.println("Please input a decimal number(0 to exit):");
    num=new java.util.Scanner(System.in).nextLong();
    if(num==0) {
            System.out.println("Zero. Exiting...");
            break;
            }

        positions[1]=".1";
        positions[2]=".2";

        int scaleIndex=1;
        for(int i=3;i<DIGIT_LIMIT;i++){
        positions[i]=scale[scaleIndex];
        scaleIndex=scaleIndex%7+1;
        }

        spellNum[1]="One";
        spellNum[2]="Two";
        spellNum[3]="Three";
        spellNum[4]="Four";
        spellNum[5]="Five";
        spellNum[6]="Six";
        spellNum[7]="Seven";
        spellNum[8]="Eight";
        spellNum[9]="Nine";
        spellNum[10]="Ten";
        spellNum[11]="Eleven";
        spellNum[12]="Twelve";
        spellNum[13]="Thirteen";
        spellNum[14]="Forteen";
        spellNum[15]="Fifteen";
        spellNum[16]="Sixteen";
        spellNum[17]="Seventeen";
        spellNum[18]="Eighteen";
        spellNum[19]="Nineteen";
        spellNum[20]="Twenty";

        spellNum[30]="Thirty";
        spellNum[40]="Forty";
        spellNum[50]="Fifty";
        spellNum[60]="Sixty";
        spellNum[70]="Seventy";
        spellNum[80]="Eighty";
        spellNum[90]="Ninety";

        tys[2]="Twenty";
        tys[3]="Thirty";
        tys[4]="Forty";
        tys[5]="Fifty";
        tys[6]="Sixty";
        tys[7]="Seventy";
        tys[8]="Eighty";
        tys[9]="Ninety";

        long tmpNum=num;
        int count=1;

        for(;tmpNum!=0;){
            digits[count]=(int)tmpNum%10;
            tmpNum=tmpNum/10;

            count=count+1;
        }

        count--;

        int outDigits[]=new int[DIGIT_LIMIT];
        String outScales[]=new String[DIGIT_LIMIT];

        int outDigitCount=0;
        int outScaleCount=0;
        int bufferDigit=0;
        String bufferScale;

        for(int i=count;i>0;i--){

            switch(positions[i]){
                case "hundred":
                    outDigits[outDigitCount++]=digits[i];
                    outScales[outScaleCount++]="Hundred";
                    break;
                case "crore2":
                    bufferDigit=digits[i];bufferScale="Crore";
                    break;
                case "crore1":
                    outDigits[outDigitCount++]=bufferDigit*10+digits[i];
                    outScales[outScaleCount++]="Crore";
                    bufferDigit=0;bufferScale=null;
                    break;
                case "lac2":
                    bufferDigit=digits[i];bufferScale="Lac";
                    break;
                case "lac1":
                    outDigits[outDigitCount++]=bufferDigit*10+digits[i];
                    outScales[outScaleCount++]="Lac";
                    bufferDigit=0;bufferScale=null;
                    break;   
                case "thousand2":
                    bufferDigit=digits[i];bufferScale="Thousand";
                    break;
                case "thousand1":
                    outDigits[outDigitCount++]=bufferDigit*10+digits[i];
                    outScales[outScaleCount++]="Thousand";
                    bufferDigit=0;bufferScale=null;
                    break;
                default:

            }

        }

        String result="";
        String lastScale=null;
        String ranks="Hundred Thousand Lac Crore";

        for(int i=0;i<outDigitCount;i++){
            
            if(outDigits[i]==0 && lastScale != null) {

                if(ranks.indexOf(outScales[i]) > ranks.indexOf(lastScale))
                result=result+outScales[i]+" ";

                lastScale=outScales[i];
                continue;
            }

            if(outDigits[i]==0 && lastScale == null) {
                result=result+outScales[i]+" ";
                lastScale=outScales[i];
                continue;
            }

            if(outDigits[i]!=0) {
                result=result+spellDigits(outDigits[i])+" ";
                result=result+outScales[i]+" ";
                lastScale=outScales[i];
            }
        }

        if(digits[2]*10+digits[1] != 0)
        result=result+spellDigits(digits[2]*10+digits[1]);

        System.out.println(result);

}
    }

}
 
 

No comments:

Post a Comment