in reply to Problem adding comma & removing decimal

sub addComma { my $number = reverse int shift; $number =~ s/(\d{3})(?=\d)(?!\d*\.)/$1,/g; return scalar reverse $number; }

Partial credit goes to the Perl Cookbook, and the rest to int instead of a RE.


Dave

Replies are listed 'Best First'.
Re^2: Problem adding comma & removing decimal
by Steny (Sexton) on Jun 05, 2004 at 06:01 UTC
    Thx a ton! That works perfectly :-)
Re^2: Problem adding comma & removing decimal
by bart (Canon) on Jun 05, 2004 at 06:17 UTC
    I don't see any significant difference with the code he was using (apart from the int).

    To the OP: I think that if it fails, it's probably to blame to the format of the "numberstring" you're passing to this sub.

      His method was failing for lack of int. That was the significant change. The negative lookahead assertion was rejecting the string when it reached the part of the string that had a decimal point. That caused the substitutions to stop before the string was done being commafied, and did nothing for eliminating the decimal portion.


      Dave