Forum Moderators: open
1000.00123
100000
12000000.00
Does anybody have a handy function that will convert them to thousand separated, like so?
1,000.00123
100,000
12,000,000.00
I've had a crack at this but my code just seems to be getting way overcomplicated (oh, and it doesn't work).
given string n which is your number and assumming:
loop through the characters in n to find the first occurence of the '.' character. Record it's index in n.
If you dont find it, set the index to be the length of n
starting from the front of n, use the mod 3 of the index of the '.' minus index of the current character to determine if a comma should be inserted behind it. loop through n until you reach the '.' character.
I took a book home and read up on them... it does make life a lot easier:
function addCommas( sValue )
{
var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})'); while(sRegExp.test(sValue)) {
sValue = sValue.replace(sRegExp, '$1,$2');
}
return sValue;
}