I saw this beautiful script to add thousands separator to js numbers:
function thousandSeparator(n, sep)
{
var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})'),
sValue = n + '';
if(sep === undefined)
{
sep = ',';
}
while(sRegExp.test(sValue))
{
sValue = sValue.replace(sRegExp, '$1' + sep + '$2');
}
return sValue;
}
usage :
thousandSeparator(5000000.125, '\,') //"5,000,000.125"
However I'm having a trouble accepting the while loop.
I was thinking to change the regex to : '(-?[0-9]+)([0-9]{3})*'
asterisk...
but now , how can I apply the replace statement ?
now I will have $1
and $2..$n
how can I enhance the replace func?
p.s. the code is taken from here http://www.grumelo.com/2009/04/06/thousand-separator-in-javascript/