2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

How can I format numbers as money in JavaScript?

I want a function which takes a price of type float as an argument and returns a String formatted like this: "$ 2,500.00"
userimage

Hi,

You can write your logic with below code.

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;  // In this u can append ur symbol.
Answer is