/    Sign up×
Community /Pin to ProfileBookmark

Javascript Problem Currency Formatting

Hi there

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.

Columbo1977

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@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>

Examples of how to invoke these:

// calculate total cost

f.cdtotalfield.value = (c = qtyBreaks[x][cost] * n).toCurrency(USAcurrency);

// calculate total tax

f.Taxtotalfield.value = Number((t = c *
taxRate).toFixed(2)).toCurrency(USAcurrency);

// calculate total shipping

f.totalsh.value = (s = qtyBreaks[x][ship] * n).toCurrency(USAcurrency);

// calcualte grand total

f.grandtotalfield.value = (c + t + s).toCurrency(USAcurrency);
Copy linkTweet thisAlerts:
@columbo1977authorMay 11.2006 — Thanks for the help mate but i havent a clue how to adapt that to what i need, im getting depressed now, lol

if you want i can email you the webpage so you can see how i have it set out? the address is [email][email protected][/email]

thanks

Columbo1977
Copy linkTweet thisAlerts:
@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:

document.forms["formName"].elements["fieldName"].value = nbr.toCurrency(USAcurrency);
×

Success!

Help @columbo1977 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...