Re: convert money(to words)
[ Follow Ups ] [ Post Followup ] [ Intranet eXchange ] [ IDM Home ]
Posted by bill palmer on March 24, 2000 at 21:10:12:
In Reply to: convert money posted by megat on March 21, 2000 at 3:0:44:
Here is a function I wrote for fun. It has no validation. You must be sure to give it a clean number as in the example. (i.e no commas, etc.) <HTML> <HEAD> <TITLE>Example of word formatted amounts</TITLE> <SCRIPT language="javascript"> //Words database needed just for 1 to 90 n0=""; n1="One"; n2="Two"; n3="Three"; n4="Four"; n5="Five"; n6="Six"; n7="Seven"; n8="Eight"; n9="Nine"; n10="Ten"; n11="Eleven"; n12="Twelve"; n13="Thirteen"; n14="Fourteen"; n15="Fifteen"; n16="Sixteen"; n17="Seventeen"; n18="Eighteen"; n19="Nineteen"; n20="Twenty"; n30="Thirty"; n40="Forty"; n50="Fifty"; n60="Sixty"; n70="Seventy"; n80="Eighty"; n90="Ninety"; //Word numbers are really expressed for 1 to 999. //We process only one 3-digit part of a number and need "aType" to say what part. //Call this function four times to get billions, millions, thousands and dollars/cents. //(i.e see function "wordAmount(a)" below which calls our wordamount999(a,t) four times.) //This function accomodates forms with no cents or billions and millions on a separate line. function wordAmount999(aAmount,aType){ typeFlag="none"; typeWord=""; if (aType.indexOf("dol")!=-1){typeFlag="dollars"}; if (aType.indexOf("cen")!=-1){typeFlag="dollars and cents"}; if (aType.indexOf("thou")!=-1){typeFlag="thousands"}; if (aType.indexOf("mill")!=-1){typeFlag="millions"}; if (aType.indexOf("bill")!=-1){typeFlag="billions"}; amountX100=parseInt(parseFloat(aAmount)*100); amountX100Txt=""+amountX100; amtWholeTxt=amountX100Txt.substring(0,amountX100Txt.length-2); amtCentsTxt=amountX100Txt.substring(amountX100Txt.length-2,amountX100Txt.length); amtCents=parseInt(amtCentsTxt); amtCentsTxt2=amtCentsTxt; if (amtCents<1){amtCentsTxt2="no"}; txtOnes=""; spc=" "; //amtWhole=parseInt(amtWholeTxt); amtWhole=0; if (typeFlag=="billions" && amtWholeTxt.length>9){ amtWholeTxt=amtWholeTxt.substring(0,amtWholeTxt.length-9) amtWhole=parseInt(amtWholeTxt); if (amtWhole % 100 > 0){typeWord="Billion"} }; if (typeFlag=="millions" && amtWholeTxt.length>6){ amtWholeTxt=amtWholeTxt.substring(0,amtWholeTxt.length-6) amtWhole=parseInt(amtWholeTxt); if (amtWhole % 100 > 0){typeWord="Million"} }; if (typeFlag=="thousands" && amtWholeTxt.length>3){ amtWholeTxt=amtWholeTxt.substring(0,amtWholeTxt.length-3) amtWhole=parseInt(amtWholeTxt); if (amtWhole % 100 > 0){typeWord="Thousand"} }; if (typeFlag=="dollars" || typeFlag=="dollars and cents"){ amtWhole=parseInt(amtWholeTxt) if (amtWhole > 0){typeWord="Dollar"} if (amtWhole > 1){typeWord="Dollars"} }; amtOnes=amtWhole % 100; amtOnesTxt=""; if (amtOnes<21){eval("amtOnesTxt="+"n"+amtOnes)}; if (amtOnes>20){ eval("amtOnesTxt="+"n"+parseInt(amtOnes / 10)+"0"); eval("amtOnesTxt=amtOnesTxt+spc+"+"n"+parseInt(amtOnes % 10)) }; amtHundredsTxt=""; if (amtWhole % 1000 > 99){ eval("amtHundredsTxt="+"n"+parseInt((amtWhole % 1000)/100)); amtHundredsTxt=amtHundredsTxt+" "+"Hundred" }; centsString=""; centsWord=&qu
Follow Ups:
|