Help with thousands separator regexp

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Help with thousands separator regexp

Post 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.

jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Re: Help with thousands separator regexp

Post by jacky »

This should do it:

Code: Select all

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

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Help with thousands separator regexp

Post 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. :(

Post Reply