How can I format numbers as dollars currency string in JavaScript?

I would like to format a price in JavaScript.
I'd like a function which takes a float as an argument and returns a string formatted like this:

$ 2,500.00 

What's the best way to do this?

Text Copied From
on : Mon, Sep 29, 2008 Views : 1260795

Accepted Answer

Ok, based on what you said, i'm using this:

var DecimalSeparator = Number(1.2).toLocaleString().substr(1,1);
  var AmountWithCommas = Amount.toLocaleString();
 var arParts = String(AmountWithCommas).split(DecimalSeparator);
 var intPart = arParts[0];
 var decPart = (arParts.length > 1 ? arParts[1] : '');
 decPart = (decPart + '00').substr(0,2);
  return '£ ' + intPart + DecimalSeparator + decPart;

I'm open to improvement suggestions (i'd prefer not to include YUI just to do this :-) ) I already know I should be detecting the . instead of just using it as the decimal separator...

Text Copied From
on : Mon, Sep 29, 2008

Ask Question

If you want to ask question you must LogIn or SignUp
Login SignUp

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp