Page 1 of 1

Help with thousands separator regexp

Posted: 15 Jan 2010 18:12
by serendipity
I want to add thousands separator to a number.

The number: 100000000000.00
Should look like: 100,000,000,000.00

I tried the below:

Code: Select all

$num= 100000000000.00;
$num= regexreplace ($num, "(-?[0-9]+)([0-9]{3})","$1,$2"); 
but that gives me 100000000,000.00

Any help?

PS: Meanwhile I am using the code 4 times in a row to achieve what i want.

Re: Help with thousands separator regexp

Posted: 15 Jan 2010 18:32
by jacky
This should do it:

Code: Select all

$num = regexreplace($num, "(\d{1,3})(?=(\d{3})+(?!\d))", "$1,");

Re: Help with thousands separator regexp

Posted: 15 Jan 2010 18:35
by serendipity
jacky wrote:This should do it:

Code: Select all

$num = regexreplace($num, "(\d{1,3})(?=(\d{3})+(?!\d))", "$1,");
:D , you're the best! Thanks.
Shame, i don't have time to learn RegExp. :(