Convert Amount in numbers to words in JavaScript

Hello All,

I have updated the script to make sure below mentioned issues are fixed. Please go ahead with your testing and let me know.


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 = ['', ' Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety'];
var inWords = [];

var numReversed, inWords, actnumber, i, j;

function tensComplication() {
	'use strict';
	if (actnumber[i] === 0) {
		inWords[j] = '';
	} else if (actnumber[i] === 1) {
		inWords[j] = ePlace[actnumber[i - 1]];
	} else {
		inWords[j] = tensPlace[actnumber[i]];
	}
}

function testSkill() {
	'use strict';
	var junkVal = document.getElementById('rupees').value;
	junkVal = Math.floor(junkVal);
	var obStr = junkVal.toString();
	numReversed = obStr.split('');
	actnumber = numReversed.reverse();

	if (Number(junkVal) >= 0) {
		//do nothing
	} else {
		window.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) {
		window.alert('Oops!!!! the Number is too big to covertes');
		return false;
	}



	var iWordsLength = numReversed.length;
	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] + ' Only';
				break;
			case 1:
				tensComplication();
				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:
				tensComplication();
				break;
			case 5:
				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] + ' Lakh';
				}
				break;
			case 6:
				tensComplication();
				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:
				tensComplication();
				break;
			default:
				break;
		}
		j++;
	}


	inWords.reverse();
	for (i = 0; i < inWords.length; i++) {
		finalWord += inWords[i];
	}
	document.getElementById('container').innerHTML = obStr + '  ' + finalWord;
}

Include the below code in your HTML


<input type="text" name="rupees" id="rupees" />
<input type="button" name="sr1" value="Click Here" onClick="testSkill()"/>

<div id="container"></div>

If you like the script then post the comments

Advertisements

51 comments

  1. thanks Sridhar Kuppalli..
    your scripts works fine for me..
    just i changed the quotes in entire sciprt…manually..
    and its work..
    how to extend this script for decimal values….

  2. Hi,
    The codes well till 99,999 but if you enter 100000 the conversion in words is “100000 One Lakh Thousand Only” whereas it should be only “one lakh only” please suggest me what should i do?

  3. Hi Thanks a lot… Its working nicely but we want to display fraction also please suggest some sol for that
    thanks thanks a lot.. lot.. lot..

  4. This code is very helpful.
    But I face one problem in this code
    when i entered 5000000 then it gives “Fifty Lakh Thousand Only”.
    and this is not right according to indian format. It will be Fifty lakh only. So plz provide this type of code if you have then mail me on my email-id
    gagan82991@gmail.com

    Thanks in advance.

      1. Hai Kuppalli,
        I want this function in paise format also … I got error for 5000000

  5. how to apply it in excel for converting rs to words . I install java in my pc. but I can not use this java script . pls reply me immediately

  6. Please simplyfy the coding and then send to my mail …(Convert Amount in numbers to words in JavaScript)
    It’s urgent sir….
    Thanks you.

  7. Please send me the simplify codeing on my mail….(Convert Amount in numbers to words in JavaScript) ….

    Thanx & Regards

  8. Dear expert,
    accept the value from the user from keyboard using promt and convert into string.how we create this program in prompt.

  9. I face one problem in this code
    when i entered 5000000 then it gives “Fifty Lakh Thousand Only”.
    and this is not right according to indian format. It will be Fifty lakh only. So plz provide this type of code if you have then mail me on my email-id

      1. How can I use this in Form field to convert Amount Entered in One Cell, to be converted in Words in other cell.

        I Have THis
        But It Only Work Till 99,999
        Above that For 1,00,000 it Gives ” one hundred Thousand Only”

        “”””””””””””””””PLEASE HELP””””””””””””””

        DOCUMENT LEVEL JAVA SCRIPT

        function ConvertToHundreds(num)
        {
        aTens = [ “Twenty”, “Thirty”, “Forty”, “Fifty”, “Sixty”, “Seventy”, “Eighty”, “Ninety”];
        aOnes = [ “Zero”, “One”, “Two”, “Three”, “Four”, “Five”, “Six”, “Seven”, “Eight”, “Nine”,
        “Ten”, “Eleven”, “Twelve”, “Thirteen”, “Fourteen”, “Fifteen”, “Sixteen”, “Seventeen”,
        “Eighteen”, “Nineteen” ];

        var cNum, nNum;
        var cWords = “”;
        num %= 1000;
        if (num > 99) {
        /* Hundreds. */
        cNum = String(num);
        nNum = Number(cNum.charAt(0));
        cWords += aOnes[nNum] + ” Hundred”;
        num %= 100;
        if (num > 0)
        cWords += ” and ”
        }

        if (num > 19) {
        /* Tens. */
        cNum = String(num);
        nNum = Number(cNum.charAt(0));
        cWords += aTens[nNum – 2];
        num %= 10;
        if (num > 0)
        cWords += “-“;
        }
        if (num > 0) {
        /* Ones and teens. */
        nNum = Math.floor(num);
        cWords += aOnes[nNum];
        }
        return cWords;
        }
        function ConvertToWords(num)
        {
        var aUnits = [ “Thousand”, “Lakh”, “Crore” ];
        var cWords = (num >= 1 && num 0; i++) {
        if (nLeft % 1000 > 0) {
        if (i != 0)
        cWords = ConvertToHundreds(nLeft) + ” ” + aUnits[i – 1] + ” ” + cWords;
        else
        cWords = ConvertToHundreds(nLeft) + ” ” + cWords;
        }
        nLeft = Math.floor(nLeft / 1000);
        }
        num = Math.round(num * 100) % 100;
        if (num > 0)
        cWords += ConvertToHundreds(num) + ” Paise”;
        else
        cWords += “Only”;
        return cWords;
        }

        CALCULATION SCRIPT FOR FORM FIELD

        // get the field with numbers
        var f = this.getField(“Number”);
        // call the convert function with the value of the number field
        event.value = ConvertToWords(f.value);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s