Monday, January 16, 2017

Convert Numbers To Words With JavaScript

Convert Numbers To Words With JavaScript
    <script>
    var s_num = ["Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety",""];

    function NumericToChar(str)
    {
       
        number = str;
        i_part = parseInt(number);
        f_part = str.slice(-2);

        var s_text = "Rupees ";

        if(i_part >= 0 && i_part <= 20)
        {
            s_text += s_num[i_part];
        }
        if(i_part >= 21 && i_part <= 99)
        {
            i_str_part = i_part.toFixed();
            ch = i_str_part.slice(0, 1);
            if(ch != "0") s_text += s_num[parseInt(ch)+18]+" ";
            ch = i_str_part.slice(1, 2);
            if(ch != "0") s_text += s_num[parseInt(ch)];
        }
        if(i_part >= 100 && i_part <= 999)
        {
            i_str_part = i_part.toFixed();
            ch = i_str_part.slice(0, 1);
            if(ch != "0") s_text += s_num[parseInt(ch)]+" Hundred ";
            ch = i_str_part.slice(1, 2);
            if(ch != "0") s_text += s_num[parseInt(ch)+18]+" ";
            ch = i_str_part.slice(2, 3);
            if(ch != "0") s_text += s_num[parseInt(ch)];
        }
        if(i_part >= 1000 && i_part <= 9999)
        {
            i_str_part = i_part.toFixed();
            ch = i_str_part.slice(0, 1);
            if(ch != "0") s_text += s_num[parseInt(ch)]+" Thousand ";
            ch = i_str_part.slice(1, 2);
            if(ch != "0") s_text += s_num[parseInt(ch)]+" Hundred ";
            ch = i_str_part.slice(2, 3);
            if(ch != "0") s_text += s_num[parseInt(ch)+18]+" ";
            ch = i_str_part.slice(3, 4);
            if(ch != "0") s_text += s_num[parseInt(ch)];
        }
        if(i_part >= 10000 && i_part <= 99999)
        {
            i_str_part = i_part.toFixed();
            s_text = GetText(i_str_part.slice(0, 2))+" Thousand ";
            ch = i_str_part.slice(2, 3);
            if(ch != "0") s_text += s_num[parseInt(ch)]+" Hundred ";
            ch = i_str_part.slice(3, 4);
            if(ch != "0") s_text += s_num[parseInt(ch)+18]+" ";
            ch = i_str_part.slice(4, 5);
            if(ch != "0") s_text += s_num[parseInt(ch)];
        }
        if(i_part >= 100000 && i_part <= 999999)
        {
            i_str_part = i_part.toFixed();
            ch = i_str_part.slice(0, 1);
            if(ch != "0") s_text += s_num[parseInt(ch)]+" Lac ";
            s_text += GetText(i_str_part.slice(1, 3))+" Thousand ";
            ch = i_str_part.slice(3, 4);
            if(ch != "0") s_text += s_num[parseInt(ch)]+" Hundred ";
            else s_text += s_num[parseInt(ch)+28]+" ";
            s_text += GetText(i_str_part.slice(4, 6));
        }
        if(i_part >= 1000000 && i_part <= 9999999)
        {
            i_str_part = i_part.toFixed();

            s_text += GetText(i_str_part.slice(0, 2))+" Lac ";
            s_text += GetText(i_str_part.slice(2, 4))+" Thousand ";
            ch = i_str_part.slice(4, 5);
            if(ch != "0") s_text += s_num[parseInt(ch)]+" Hundred ";
            ch = i_str_part.slice(5, 6);
            if(ch != "0") s_text += s_num[parseInt(ch)+18]+" ";
            ch = i_str_part.slice(6, 7);
            if(ch != "0") s_text += s_num[parseInt(ch)];
        }
        if(i_part >= 10000000 && i_part <= 99999999)
        {
            i_str_part = i_part.toFixed();
            ch = i_str_part.slice(0, 1);
            if(ch != "0") s_text += s_num[parseInt(ch)]+" Crore ";
            s_text += GetText(i_str_part.slice(1, 3))+" Lac ";
            s_text += GetText(i_str_part.slice(3, 5))+" Thousand ";
            ch = i_str_part.slice(5, 6);
            if(ch != "0") s_text += s_num[parseInt(ch)]+" Hundred ";
            else s_text += s_num[parseInt(ch)+28]+" ";
            s_text += GetText(i_str_part.slice(6, 8));
        }

        s_text += " and ";

        ch = f_part.slice(0, 1);
        if(ch == "0")
        {
            ch = f_part.slice(1, 2);
            if(ch != "0") s_text += s_num[parseInt(ch)]+" ";
            else s_text += "Zero";
        }
        else
        {
            s_text += GetText(f_part.slice(0, 2));
        }

        s_text += " Paise Only";
        return s_text;
    }

    function GetText(ch)
    {
            str = ch;
            i_part = parseInt(str);

            if(i_part >= 0 && i_part <=20)
                s_text = s_num[i_part];

            if(i_part >= 21 && i_part <= 99)
            {
                ch = str.slice(0, 1);
                if(ch != "0") s_text = s_num[parseInt(ch)+18];
                ch = str.slice(1, 2);
                if(ch != "0") s_text += " "+s_num[parseInt(ch)];
            }

        return s_text;
    }

    document.write(NumericToChar('99999999.99'));
    </script>

No comments:

Post a Comment