I want to format numbers using JavaScript.
For example:
10 => 10.00
100 => 100.00
1000 => 1,000.00
10000 => 10,000.00
100000 => 100,000.00
I want to format numbers using JavaScript.
For example:
10 => 10.00
100 => 100.00
1000 => 1,000.00
10000 => 10,000.00
100000 => 100,000.00
I think with this jQuery-numberformatter you could solve your problem.
Of course, this is assuming that you don't have problem with using jQuery in your project.
Source: http://jsperf.com/number-format
Short solution:
Use
Where 2 is the number of decimal places
Edit:
Here's the function to format number as you want
Sorce: www.mredkj.com
Output
Due to the bugs found by JasperV — good points! — I have rewritten my old code. I guess I only ever used this for positive values with two decimal places.
Depending on what you are trying to achieve, you may want rounding or not, so here are two versions split across that divide.
First up, with rounding.
I've introduced the
toFixed()
method as it better handles rounding to specific decimal places accurately and is well support. It does slow things down however.This version still detaches the decimal, but using a different method than before. The
w|0
part removes the decimal. For more information on that, this is a good answer. This then leaves the remaining integer, stores it ink
and then subtracts it again from the original number, leaving the decimal by itself.Also, if we're to take negative numbers into account, we need to while loop (skipping three digits) until we hit
b
. This has been calculated to be 1 when dealing with negative numbers to avoid putting something like-,100.00
The rest of the loop is the same as before.
In the snippet below you can edit the numbers to test yourself.
Now the other version, without rounding.
This takes a different route and attempts to avoid mathematical calculation (as this can introduce rounding, or rounding errors). If you don't want rounding, then you are only dealing with things as a string i.e. 1000.999 converted to two decimal places will only ever be 1000.99 and not 1001.00.
This method avoids using
.split()
andRegExp()
however, both of which are very slow in comparison. And whilst I learned something new from Michael's answer abouttoLocaleString
, I also was surprised to learn that it is — by quite a way — the slowest method out of them all (at least in Firefox and Chrome; Mac OSX).Using
lastIndexOf()
we find the possibly existent decimal point, and from there everything else is pretty much the same. Save for the padding with extra 0s where needed. This code is limited to 5 decimal places. Out of my test this was the faster method.I'll update with an in-page snippet demo shortly, but for now here is a fiddle:
https://jsfiddle.net/bv2ort0a/2/
Old Method
Why use RegExp for this? — don't use a hammer when a toothpick will do i.e. use string manipulation:
walk through
First strip off decimal:
Work backwards from the end of the string:
Finalise by adding the leftover prefix and the decimal suffix (with rounding to
dp
no. decimal points):fiddlesticks
http://jsfiddle.net/XC3sS/