Perlfunc: Commify
Simple perl function to add commas to a long number to make it more readable.
sub commify {
# commify a number. Perl Cookbook, 2.17, p. 64
my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scalar reverse $text;
}
Leave A Reply
- 3 Replies
Replies
May 19th 2010 - Larry Scott B.
Here's another (faster) method:
sub commify {
local $_ = shift;
1 while s/^(-?\d+)(\d{3})/$1,$2/;
return $_;
}
July 30th 2010 - Andy
That just did n,n,n,n,n,n,n Try this instead:
sub commify {
local $_ = shift;
1 while s/^(-?\d+)(\d{3})/$1,$2/;
return $_;
}
July 30th 2010 - Andy
damned filters! Please fix!
Second \d should be followed immediately by leftbrace 3 rightbrace
sub commify {
local $_ = shift;
1 while s/^(-?\d+)(\d{3})/$1,$2/;
return $_;
}