in reply to How do I add commas to a number?

sub addcommas{ my ($text)=@_; $text=reverse $text; $text=~s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; $text=scalar reverse $text; return $text; }

Replies are listed 'Best First'.
Re: Answer: How do I add commas to a number?
by Anonymous Monk on Dec 12, 2001 at 20:39 UTC
    Very inefficient! Use:
    $commas=~ s/^(-?\d+)(\d{3})/$1,$2/;
    One line does it all.

    hawk@rthawk.com

      What happens if your value is 1_000_000 ?

      "Livet är hårt" sa bonden.
      "Grymt" sa grisen...

      A reply falls below the community's threshold of quality. You may see it by logging in.