I was wondering if anyone could help me, I have an order form that has a quantity and cost fields and a total at the bottom i was wondering if anyone would know how to apply formatting to the cost fields and the total field so they can appear as currency.
@phpnoviceMay 11.2006 — #I read your question, from work, several hours ago but didn't have my code with me to offer you at that time. I'm surprised noone has answered you in the meantime. At any rate... Here are my functions both for adding formatting and removing it: <script type="text/javascript"> <!--// String.prototype.toNumber = function() { // convert number-formatted string into a numeric result var nbr = Number(this); return ((isNaN(nbr)) ? 0 : nbr); } String.prototype.fromCurrency = function() { // convert currency-formatted string into a numeric result var symb = (arguments.length>0) ? arguments[0] : "$,."; symb = ((typeof(symb)=="string" && symb.length>1) ? symb : "$,.").split(""); var re = new RegExp("[\"+symb[0]+"\"+symb[1]+"]","g"); return this.replace(re,"").toNumber(); } Number.prototype.toCurrency = function() { // convert number into a currency-formatted string result var symb = (arguments.length>0) ? arguments[0] : "$,."; symb = ((typeof(symb)=="string" && symb.length==3) ? symb : "$,.").split(""); var nbr = this.toString().toNumber().toString(); dec = Math.floor((nbr*100+0.5)%100); nbr = Math.floor((nbr*100+0.5)/100).toString(); if (dec < 10) dec = "0" + dec; for (var i = 0; i < Math.floor((nbr.length-(1+i))/3); i++) { nbr = nbr.substring(0,nbr.length-(4*i+3))+symb[1]+nbr.substring(nbr.length-(4*i+3)); } return (symb[0] + nbr + symb[2] + dec); } var USAcurrency = "$,."; //--> </script>
@phpnoviceMay 11.2006 — #I come to this site to help out for free. If it turns into direct consultation, then there is a fee for my services. But, it's not that hard... If you have a numeric variable (let's call it [B]nbr[/B]) which contains the amount you wish to convert to a currency format and put that into a form field, then this will do it: