Perl doesn't see commas as being part of a number?
Given that it is a string that is less than suprising. In fact Perl tries its best to make a number out of the string - in fact it does find the number 1 (the bit before the first comma).....
C:\>perl -e "print '1,000,000' + 1000"
1001
C:\>
What you see is a warning as Perl knows that effectively doing an atoi()ish thing on a string may give unexpected results. Just remove the commas first and re-add them for output.
sub decommify {
my ( $number ) = @_;
$number =~ tr/,//d;
return $number
}
sub commify {
my ( $number ) = @_;
return undef unless $number;
( $number, my $dec ) = $number =~ m!([+-]?\d+)\.?(\d*)!;
return undef unless $number;
$number =~ s/(\d)(?=(\d{3})+(\D|$))/$1,/g if length($number) > 3;
return $dec ? "$number.$dec" : $number;
}
print commify( decommify("1,000,001.123456789") + 1000 );
__DATA__
1,001,001.12345679
|