Friday, 13 December 2013

Convert Amount in numbers to words in JavaScript

 <asp:TextBox ID="txtsumassured" runat="server" MaxLength="15"                                       onkeyup="AN_toWords(this.value, 'lblnumtoword', 'lblerrsumass', 'Imgerriconsumass')></asp:TextBox>



//----------It is not applicable for More than 15 digits ---------------------------//
function AN_toWords(MyVal, lblnumbertoword, errmess, erricon) {
    document.getElementById(lblnumbertoword).innerHTML = "";
    var rVal = MyVal;
    var numbers = /^[0-9\,]+$/;
    MyVal = MyVal.toString();
    MyVal = MyVal.replace(/[\, ]/g, '');
    var tempnew = MyVal;
    if (tempnew == "") {
        document.getElementById(errmess).innerHTML = "Please enter the single premium";
        return false;
    }
    else if (!numbers.test(tempnew)) {
        document.getElementById(errmess).innerHTML = "Please enter a valid amount";
            return false;
        }

    rVal = Math.floor(tempnew);
    var rup = new String(rVal);

    rupRev = rup.split("");
    actualNumber = rupRev.reverse();

    if (Number(rVal) >= 0) {

    }
    else {
        alert('Number cannot be converted');
        return false;
    }
    if (Number(rVal) == 0) {
        document.getElementById(lblnumbertoword).innerHTML = 'Zero Only';
        return false;
    }


    var numWords = ["Zero", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine"];
    var numPlace = ['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'];
    var tPlace = ['dummy', ' Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety'];

    var numWordsLength = rupRev.length;
    var totalWords = "";
    var numtoWords = new Array();
    var finalWord = "";
    j = 0;
    for (i = 0; i < numWordsLength; i++) {
        switch (i) {
            case 0:
                if (actualNumber[i] == 0 || actualNumber[i + 1] == 1) {
                    numtoWords[j] = '';
                }
                else {
                    numtoWords[j] = numWords[actualNumber[i]];
                }
                numtoWords[j] = numtoWords[j] + ' Only';
                break;
            case 1:
                CTen();
                break;
            case 2:
                if (actualNumber[i] == 0) {
                    numtoWords[j] = '';
                }
                else if (actualNumber[i - 1] != 0 && actualNumber[i - 2] != 0) {
                    numtoWords[j] = numWords[actualNumber[i]] + ' Hundred and';
                }
                else {
                    numtoWords[j] = numWords[actualNumber[i]] + ' Hundred';
                }
                break;
            case 3:
                if (actualNumber[i] == 0 || actualNumber[i + 1] == 1) {
                    numtoWords[j] = '';
                }
                else {
                    numtoWords[j] = numWords[actualNumber[i]];
                }
                if (actualNumber[i + 1] != 0 || actualNumber[i] > 0) {
                    numtoWords[j] = numtoWords[j] + " Thousand";
                }
                break;
            case 4:
                CTen();
                break;
            case 5:
                if (actualNumber[i] == 0 || actualNumber[i + 1] == 1) {
                    numtoWords[j] = '';
                }
                else {
                    numtoWords[j] = numWords[actualNumber[i]];
                }
                if (actualNumber[i + 1] != 0 || actualNumber[i] > 0) {
                    numtoWords[j] = numtoWords[j] + " Lakh";
                }
                break;
            case 6:
                CTen();
                break;
            case 7:
                if (actualNumber[i] == 0 || actualNumber[i + 1] == 1) {
                    numtoWords[j] = '';
                }
                else {
                    numtoWords[j] = numWords[actualNumber[i]];
                }
                numtoWords[j] = numtoWords[j] + " Crore";
                break;
            case 8:
                CTen();
                break;
            case 9:
                if (actualNumber[i] == 0 || actualNumber[i + 1] == 1) {
                    numtoWords[j] = '';
                }
                else {
                    numtoWords[j] = numWords[actualNumber[i]];
                }
                if (actualNumber[i + 1] != 0 || actualNumber[i] > 0) {
                    numtoWords[j] = numtoWords[j] + " Hundred ";
                }
                break;
            case 10:
                CTen();
                break;
            case 11:
                if (actualNumber[i] == 0 || actualNumber[i + 1] == 1) {
                    numtoWords[j] = '';
                }
                else {
                    numtoWords[j] = numWords[actualNumber[i]];
                }
                if (actualNumber[i + 1] != 0 || actualNumber[i] > 0) {
                    numtoWords[j] = numtoWords[j] + " Thousand ";
                }
                break;
            case 12:
                CTen();
                break;
            case 13:
                if (actualNumber[i] == 0 || actualNumber[i + 1] == 1) {
                    numtoWords[j] = '';
                }
                else {
                    numtoWords[j] = numWords[actualNumber[i]];
                }
                if (actualNumber[i + 1] != 0 || actualNumber[i] > 0) {
                    numtoWords[j] = numtoWords[j] + " Lakh ";
                }
                break;
            case 14:
                CTen();
                break;
            default:
                break;
        }
        j++;
    }

    function CTen() {
        if (actualNumber[i] == 0) {
            numtoWords[j] = '';
        }
        else if (actualNumber[i] == 1) {
            numtoWords[j] = numPlace[actualNumber[i - 1]];
        }
        else {
            numtoWords[j] = tPlace[actualNumber[i]];
        }
    }
    numtoWords.reverse();
    for (i = 0; i < numtoWords.length; i++) {
        finalWord += numtoWords[i];
    }
    document.getElementById(lblnumbertoword).innerHTML = finalWord;
}


3 comments:

  1. Thanks a bunch for sharing this with all folks you really realize what you are talking approximately! Bookmarked. Kindly also visit my site =). We will have a hyperlink trade contract among us! Go on Bliss Hosting for the best hosting in all over the world

    ReplyDelete
  2. Compare Our Dedicated Servers With Others.

    USA Missouri Dedicated Servers 16$/Month

    http://blisshostingco.net/usa-dedicated-hosting.html

    ReplyDelete