tags:

views:

231

answers:

4

I want to convert 2333444 to 2,333,444, but I'm not clear on how to have the expression work its way from the right to the left as opposed to the other way around. I'm writing Perl, but any regEx syntax is fine, I'll convert it if you are more comfortable with java or javascript.

+4  A: 

Check out Number::Format. This module provides a wide variety of number formatting solutions. e.g. for what you require

  format_number(1234567.89, 2)

yields '1,234,567.89'

Don't forget that what you're trying to do is locale-specific, and this module will handle that for you. A simple regexp solution won't do that on it's own.

Brian Agnew
Not sure why everyone is marking this (and the next) answer up? This is not an answer to my question. It's an alternative solution, which, as it happens, doesn't work for me since I am working as much on the client as on the server. Since when is regEx "reinventing the wheel". I appreciate the fact that there's a library that does this in perl, but that's not what I was asking.
Dr.Dredel
+3  A: 

You want to use the Number:Format extension, not a regular expression. You'll want to specify THOUSANDS_SEP= ','.

Bill the Lizard
+1  A: 
s/(?<=\d)(?=(\d\d\d)+(?!\d))/,/g
pb
+1  A: 

s/(?<=\d)(?=(\d\d\d)+(?!\d))/,/g

this will do it in perl style regexs

ennuikiller