Monday, November 23, 2009

Javascript to convert indian rupees in numbers to words

The following Javascript function is used to convert indian rupees in numbers to words

function test_skill() {
    var junkVal=document.getElementById('rupees').value;
    junkVal=Math.floor(junkVal);
    var obStr=new String(junkVal);
    numReversed=obStr.split("");
    actnumber=numReversed.reverse();

    if(Number(junkVal) >=0){
        //do nothing
    }
    else{
        alert('wrong Number cannot be converted');
        return false;
    }
    if(Number(junkVal)==0){
        document.getElementById('container').innerHTML=obStr+''+'Rupees Zero Only';
        return false;
    }
    if(actnumber.length>9){
        alert('Oops!!!! the Number is too big to covertes');
        return false;
    }

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

    var iWordsLength=numReversed.length;
    var totalWords="";
    var inWords=new Array();
    var finalWord="";
    j=0;
    for(i=0; i<iWordsLength; i++){
        switch(i)
        {
        case 0:
            if(actnumber[i]==0 || actnumber[i+1]==1 ) {
                inWords[j]='';
            }
            else {
                inWords[j]=iWords[actnumber[i]];
            }
            inWords[j]=inWords[j];
            break;
        case 1:
            tens_complication();
            break;
        case 2:
            if(actnumber[i]==0) {
                inWords[j]='';
            }
            else if(actnumber[i-1]!=0 && actnumber[i-2]!=0) {
                inWords[j]=iWords[actnumber[i]]+' Hundred and';
            }
            else {
                inWords[j]=iWords[actnumber[i]]+' Hundred';
            }
            break;
        case 3:
            if(actnumber[i]==0 || actnumber[i+1]==1) {
                inWords[j]='';
            }
            else {
                inWords[j]=iWords[actnumber[i]];
            }
            if(actnumber[i+1] != 0 || actnumber[i] > 0){
                inWords[j]=inWords[j]+" Thousand";
            }
            break;
        case 4:
            tens_complication();
            break;
        case 5:
            if(actnumber[i]==0 || actnumber[i+1]==1 ) {
                inWords[j]='';
            }
            else {
                inWords[j]=iWords[actnumber[i]];
            }
            inWords[j]=inWords[j]+" Lakh";
            break;
        case 6:
            tens_complication();
            break;
        case 7:
            if(actnumber[i]==0 || actnumber[i+1]==1 ){
                inWords[j]='';
            }
            else {
                inWords[j]=iWords[actnumber[i]];
            }
            inWords[j]=inWords[j]+" Crore";
            break;
        case 8:
            tens_complication();
            break;
        default:
            break;
        }
        j++;
    }

    function tens_complication() {
        if(actnumber[i]==0) {
            inWords[j]='';
        }
        else if(actnumber[i]==1) {
            inWords[j]=ePlace[actnumber[i-1]];
        }
        else {
            inWords[j]=tensPlace[actnumber[i]];
        }
    }
    inWords.reverse();
    for(i=0; i<inWords.length; i++) {
        finalWord+=inWords[i];
    }
        return finalWord;
    //document.getElementById('container').innerHTML=obStr+'  '+;
      
}

function paisa_conver(){
        var finalWord1 = test_skill();
            var finalWord2;
    var val = document.getElementById('rupees').value;
        if(val.indexOf('.')!=-1)
    {
          val = val.substring(val.indexOf('.')+1,val.length);
              if(val.length==0){
           finalWord2 = "zero paisa only";
          }
              else{
               document.getElementById('rupees').value = val;
           finalWord2 = test_skill() + " paisa only";
              }
    }
        else{
          finalWord2 = "zero paisa only";
    }
    document.getElementById('container').innerHTML=finalWord1 +" and "+finalWord2;
  
}

5 comments:

  1. Very good of you to publish this script. Glad to see someone respecting Indian money. Can you guide me how to use this script to get results on the cheque writer in PDF format I designed. I've already copied a current date function into it!! My email ID is
    sjohnbe@gmail.com

    ReplyDelete
  2. its not working

    ReplyDelete
  3. It has a bug. It works fine except when u enter 1 Crore i.e. 10000000.

    ReplyDelete
  4. i m using this JS in birt Report will u tell me where i add my element for giving value in it.

    ReplyDelete
  5. You can get a nice script for the same thing in one more site http://www.programsformca.com/2011/10/converting-number-to-words-javascript.html
    It is working with a alert box also.

    ReplyDelete