function convertNumberToWords(amount) {
var junkVal = amount;
junkVal = Math.floor(junkVal);
var obStr = new String(junkVal);
numReversed = obStr.split("");
actnumber = numReversed.reverse();
if (Number(junkVal) >= 0) {
}
else {
alert('wrong Number cannot be converted');
return '';
}
if (Number(junkVal) == 0) {
return 'Zero';
}
if (actnumber.length > 12) {
alert('Cannot convert ' + actnumber.length + ' digit number');
return '';
}
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 hundsPlace = ['dummy', ' One Hundred', ' Two Hundred', ' Three Hundred', ' Four Hundred', ' Five Hundred', ' Six Hundred', ' Seven Hundred', ' Eight Hundred', ' Nine Hundred'];
var thousPlace = ['dummy', ' One Thousand', ' Two Thousand', ' Three Thousand', ' Four Thousand', ' Five Thousand', ' Six Thousand', ' Seven Thousand', ' Eight Thousand', ' Nine Thousand'];
var tensthousPlace = ['dummy', ' Ten Thousand', ' Twenty Thousand', ' Thirty Thousand', ' Forty Thousand', ' Fifty Thousand', ' Sixty Thousand', ' Seventy Thousand', ' Eighty Thousand', ' Ninety Thousand'];
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 ';
}
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;
case 9:
if (actnumber[i] == 0 || actnumber[i + 1] == 1) {
inWords[j] = '';
}
else {
inWords[j] = hundsPlace[actnumber[i]];
}
break;
case 10:
if (actnumber[i] == 0 || actnumber[i + 1] == 1) {
inWords[j] = '';
}
else if (i == iWordsLength-1 && actnumber[i] > 0) {
inWords[j] = iWords[actnumber[i]] + ' Thousand';
}
else if (actnumber[i + 1] > 0 && actnumber[i] > 0) {
inWords[j] = tensPlace[actnumber[i + 1]] + iWords[actnumber[i]] + ' Thousand';
}
else {
inWords[j] = thousPlace[actnumber[i]];
}
break;
case 11:
if (actnumber[i] == 0 || actnumber[i + 1] == 1) {
inWords[j] = '';
}
else if (actnumber[i - 1] == 0) {
inWords[j] = tensthousPlace[actnumber[i]];
}
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++) {
switch (inWords[i]) {
case " Thousand":
if (i > 0) {
if (inWords[i - 1] != "")
finalWord += inWords[i];
}
else
finalWord += inWords[i];
break;
case " Lakh":
if (i > 0) {
if (inWords[i - 1] != "")
finalWord += inWords[i];
}
else
finalWord += inWords[i];
break;
case " Crore":
if (i > 0) {
if (inWords[i - 1] != "")
finalWord += inWords[i];
else
finalWord += inWords[i];
}
else
finalWord += inWords[i];
break;
default:
finalWord += inWords[i];
break;
}
}
return finalWord;
}