I'll look more into it later, but my head is scrabble from all the Regular Expressions...
Thanks in advance!
Moderator: General Moderators
Err. Sorry, it has to be with javascript. I'm sorry I didn't specify that earlier.The Ninja Space Goat wrote:http://us.php.net/number_format
Code: Select all
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}YOU BETTER BE SORRY!!!The Ninja Space Goat wrote:sorry I didn't notice this was in client side.
Code: Select all
function addCommas(nStr)
{
// create an empty string
nStr += '';
// create an array with 2 parts: the part to the left of the decimal, and the part to the right
// anything to the right of the decimal should be saved for later, as it's not comma'd
x = nStr.split('.');
x1 = x[0]; // the left portion (the part we're changing)
x2 = x.length > 1 ? '.' + x[1] : ''; // the right portion (the part we're leaving alone)
// here we're building a regex string to match 2 groups of numbers, 3 on the right,
// and an unlimited number before that
var rgx = /(\d+)(\d{3})/;
// as long as that pattern matches the string we're changing....
while (rgx.test(x1)) {
// add a comma between groups 1 and 2
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
// finally, add the decimal part of the string back on and return the entire string.
return x1 + x2;
}