On Thu, 5 Aug 2004 [EMAIL PROTECTED] wrote:
but then want to display the number with the commas or spaces put back
in to make it easier to read.
What do you suggest?
Two minutes of Googling found dozens of suggestions.
This may be the best one:
How can I output my numbers with commas added?
This subroutine will add commas to your number:
sub commify {
local $_ = shift;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
}
This regex from Benjamin Goldberg will add commas to numbers:
s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
It is easier to see with comments:
s/(
^[-+]? # beginning of number.
\d{1,3}? # first digits before first comma
(?= # followed by, (but not included in the match) :
(?>(?:\d{3})+) # some positive multiple of three digits.
(?!\d) # an *exact* multiple, not x * 3 + 1 or whatever.
)
| # or:
\G\d{3} # after the last group, get three digits
(?=\d) # but they have to have more digits after them.
)/$1,/xg;
<http://search.cpan.org/~nwclark/perl-5.8.5/pod/perlfaq5.pod#How_can_I_output_my_numbers_with_commas_added?>
Google and PerlFaq are your friends.
--
Chris Devers [EMAIL PROTECTED]
http://devers.homeip.net:8080/blog/
np: 'Mr. Lucky'
by Henry Mancini
from 'The Best Of Mancini'
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>