in reply to Re^2: "Commifying" a number
in thread "Commifying" a number

that almost works

Nice catch; should have been obvious when I wrote it.

Btw, your lookahead assertion needn’t be quantified: (?=\d) will work exactly as (?=\d+) does, but won’t waste time matching extra digits.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^4: "Commifying" a number
by Anonymous Monk on Aug 13, 2009 at 14:47 UTC
    Here is a version without using reverse function. sub commify { (my $number = shift) =~ s/\G(-?)(\d{1,3})(?=(?:\d\d\d)+(?:\.|$))/$1$2,/g; return $number; } Manikanthan Velayutham programmed to think BIG
      My earlier post did not appear properly.
      sub commify { (my $number = shift) =~ s/\G([+|-]?)(\d{1,3})(?=(?:\d\d\d)+(?:\.|$ +))/$1$2,/g; return $number; }
      Manikanthan Velayutham

      programmed to think BIG