»
EnglishFrenchVietnamese

Print - Get Number In Words - JavaScriptBank.com

Full version: jsB@nk » Multimedia » Get Number In Words
URL: https://www.javascriptbank.com/get-number-in-words.html

Get Number In Words © JavaScriptBank.comEnter a number and this JavaScript will write it out in plain English.

Full version: jsB@nk » Multimedia » Get Number In Words
URL: https://www.javascriptbank.com/get-number-in-words.html



JavaScript
<SCRIPT language=JavaScript><!--var flag = false;var flag_6 = false;var arr_unit = new Array("","One","Two","Three","Four","Five","Six","Seven","Eight","Nine");var arr_hundred = new Array("","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety");var arr_spl = new Array("","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen");function getAmountInWords(str_amount){var str_part = "";var str_returnvalue = "";var str_words_rs = "";var str_words_ps = "";var str_token_1 = "";flag_6 = false;if (parseFloat(str_amount) == 0)alert("Please Enter some Amount");elseif (str_amount.substr(0,1) == '0')alert("Please Enter valid Amount");else{var ind = str_amount.indexOf(".");if (ind != -1){str_token_1 = str_amount.substring(0,ind);str_token_2 = str_amount.substring(ind+1,str_amount.length);if (str_token_2.length == 1)str_token_2 = str_token_2 + "0";}else{str_token_1 = str_amount;str_token_2 = "";}for (var i=0;i<str_token_1.length;i++){str_part = str_token_1.substring(i,str_token_1.length);str_returnvalue = getWords(str_part);if (str_returnvalue != "")str_words_rs += str_returnvalue + " ";if (flag)i++;}if (str_token_2 != "" && str_token_2 != "00"){for (var i=0;i<str_token_2.length;i++){str_part = str_token_2.substring(i,str_token_2.length);str_returnvalue = getWords(str_part);if (str_returnvalue != "")str_words_ps += str_returnvalue + " ";if (flag)i++;}}if (str_token_2 != "" && str_token_2 != "00")document.forms[0].txt_words.value = "Rupees " + str_words_rs + " And "+str_words_ps + "Paise" + " Only";elsedocument.forms[0].txt_words.value = "Rupees "+ str_words_rs + " Only";}}//end function getAmountInWords(str_amount)function getWords(str_part){var val_1 = "";var val_2 = "";var str_returnvalue = "";flag = false;var k = str_part.length;switch(k){case 0 :case 1 :val_1 = parseInt(str_part.substr(0,1));str_returnvalue = arr_unit[val_1];break;case 2 :val_1 = parseInt(str_part.substr(0,1));val_2 = parseInt(str_part.substr(1,1));if (val_1 == 1 && val_2 != 0){str_returnvalue = arr_spl[val_2];flag = true;}elsestr_returnvalue = arr_hundred[val_1];break;case 3 :val_1 = parseInt(str_part.substr(0,1));if (val_1 == 0)str_returnvalue = "";elsestr_returnvalue = arr_unit[val_1] + " " + "Hundred";break;case 4 :val_1 = parseInt(str_part.substr(0,1));if (val_1 == 0 && flag_6 == true)str_returnvalue = arr_unit[val_1] ;elsestr_returnvalue = arr_unit[val_1] + " " + "Thousand";break;case 5 :val_1 = parseInt(str_part.substr(0,1));val_2 = parseInt(str_part.substr(1,1));if (val_1 == 1 && val_2 != 0){str_returnvalue = arr_spl[val_2] + " " +"Thousand";flag = true;}else{str_returnvalue = arr_hundred[val_1];}break;case 6 :val_1 = parseInt(str_part.substr(0,1));val_2 = parseInt(str_part.substr(1,1));str_returnvalue = arr_unit[val_1] + " " + "Lakh";if (val_2 == 0)flag_6 = true;break;case 7 :val_1 = parseInt(str_part.substr(0,1));val_2 = parseInt(str_part.substr(1,1));if (val_1 == 1 && val_2 != 0){str_returnvalue = arr_spl[val_2] + " " +"Lakh";flag = true;flag_6 = true;}else{str_returnvalue = arr_hundred[val_1];}break;}//end switchreturn str_returnvalue;}//end function getWords(str_part)function checkNum(newObj){var flag = true;var v_value = newObj.value;for (var i=0;i<v_value.length;i++){var v_part = v_value.substring(i,i+1);if (v_part == "e" || v_part == "E"){alert("Please enter numeric values");newObj.value = '0.00';newObj.focus();newObj.select();flag =  false;}}if (v_value == ""){alert("Please enter positive numeric values");newfield.value = "0.00";newfield.focus();newfield.select();return false ;}elseif (isNaN(v_value) || v_value <= 0){alert("Please enter positive numeric values");newObj.value = "0.00";newObj.focus();newObj.select();flag =  false;}return flag;}function checkForDecimalInAmount(newObj){var leftPart;var rightPart;var idOfDecimal;var val = newObj.value;var len = val.length;if (val.indexOf('.') == -1){if (len > 7){alert('Amount too large');newObj.value = '0.00';newObj.focus();newObj.select();return false;}}else{idOfDecimal = val.indexOf('.');leftPart = val.substring(0,idOfDecimal);rightPart = val.substring(idOfDecimal+1,val.length);if (leftPart.length > 7){alert('Amount too large');newObj.value = '0.00';newObj.focus();newObj.select();return false;}if (rightPart.length > 2){alert('Only 2 digits after decimal is allowed');newObj.value = '0.00';newObj.focus();newObj.select();return false;}}return true;}//end function checkForDecimalInAmount()//--></SCRIPT><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<FORM action="" method=post>    <INPUT style="TEXT-ALIGN: right" onfocus=this.select() maxLength=10 name=txt_amount size="20">     <INPUT onclick="if (checkNum(txt_amount)) { if (checkForDecimalInAmount(txt_amount)) getAmountInWords(txt_amount.value) }" type=button value="Get Value In Words">    <INPUT style="COLOR: red" readOnly size=100 name=txt_words></FORM><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->