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?
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?
A shorter method (for inserting space, comma or point) with regular expression ?
Short and fast solution (works everywhere!)
The idea behind this solution is replacing matched sections with first match and comma, i.e.
'$&,'
. The matching is done using lookahead approach. You may read the expression as "match a number if it is followed by a sequence of three number sets (one or more) and a dot".TESTS:
DEMO: http://jsfiddle.net/hAfMM/9571/
Extended short solution
You can also extend the prototype of
Number
object to add additional support of any number of decimals[0 .. n]
and the size of number groups[0 .. x]
:DEMO / TESTS: http://jsfiddle.net/hAfMM/435/
Super extended short solution
In this super extended version you may set different delimiter types:
DEMO / TESTS: http://jsfiddle.net/hAfMM/612/
A simple option for proper comma placement by reversing the string first and basic regexp.
This might be a little late, but here's a method I just worked up for a coworker to add a locale-aware
.toCurrencyString()
function to all numbers. The internalization is for number grouping only, NOT the currency sign - if you're outputting dollars, use"$"
as supplied, because$123 4567
in Japan or China is the same number of USD as$1,234,567
is here in the US. If you're outputting euro/etc., then change the currency sign from"$"
.Declare this anywhere in your HEAD or wherever necessary, just before you need to use it:
Then you're done! Use
(number).toCurrencyString()
anywhere you need to output the number as currency.As usually, there are multiple ways of doing the same thing but I would avoid using
Number.prototype.toLocaleString
since it can return different values based on the user settings.I also don't recommend extending the
Number.prototype
- extending native objects prototypes is a bad practice since it can cause conflicts with other people code (e.g. libraries/frameworks/plugins) and may not be compatible with future JavaScript implementations/versions.I believe that Regular Expressions are the best approach for the problem, here is my implementation:
edited on 2010/08/30: added option to set number of decimal digits. edited on 2011/08/23: added option to set number of decimal digits to zero.
Intl.numberformat
Javascript has a number formatter (part of the Internationalization API).
JS fiddle
Use
undefined
in place of the first argument ('en-US'
in the example) to use the system locale (the user locale in case the code is running in a browser).Intl.NumberFormat vs Number.prototype.toLocaleString
A final note comparing this to the older .
toLocaleString
. They both offer essentially the same functionality. However, toLocaleString in its older incarnations (pre-Intl) does not actually support locales: it uses the system locale. Therefore, to be sure that you're using the correct version, MDN suggests to check for the existence ofIntl
. So if you need to check for Intl anyway, why not use it instead? However, if you choose to use the shim, that also patchestoLocaleString
, so in that case you can use it without any hassle:Some notes on browser support