Contributed by Zombie miblo
on Oct 09, 2001 at 15:28 UTC
Q&A
> data formatting
Answer: How do I print a large integer with thousands separators? contributed by davorg
FAQ! Take a look at perlfaq5.
It references Number::Format, and also shows code for a regex and a subroutine, if you'd rather not use the module.
| Answer: How do I print a large integer with thousands separators? contributed by stefp $number =~ s/(\d{1,3}?)(?=(\d{3})+$)/$1,/g;
The lookahead (?=(\d{3})+$) ensures that the number of digits before each underscore we insert is a multiple of 3.
An extra set of parentheses fools Perl because the regex parser barks when there are two quantifiers in a row, which is perfectly legitimate here.
This issue was also discussed in the thread Splitting every 3 digits?.
| Answer: How do I print a large integer with thousands separators? contributed by dbwiz $number =~ s/(?<=\d)(?=(?:\d\d\d)+\b)/,/g;
| Answer: How do I print a large integer with thousands separators? contributed by Roger for ( $number )
{
/\./
? s/(?<=\d)(?=(\d{3})+(?:\.))/,/g
: s/(?<=\d)(?=(\d{3})+(?!\d))/,/g;
}
This says if the number has a decimal point,
end there for the purposes of commafication.
Otherwise, go to the end of the string.
| Answer: How do I print a large integer with thousands separators? contributed by Jaap
Here's a simple (and quite fast) one using substr:
my $number = 132452345;
for ( my $i = -3; $i > -1 * length($number); $i -= 4 )
{
substr( $number, $i, 0 ) = ',';
}
| Answer: How do I print a large integer with thousands separators? contributed by Anonymous Monk
For integers, one can avoid reverse as well as the repeated
substr derivation of the remaining string in the loop:
sub commafy_int
{
my $n = shift;
length($n) > 3 or return $n;
my $l = length($n) - 3;
my $i = ($l - 1) % 3 + 1;
my $x = substr($n,0,$i) . ',';
while ( $i < $l )
{
$x .= substr($n,$i,3) . ',';
$i += 3;
}
$x . substr($n,$i)
}
Not using reverse also makes it much easier to convert this to work
on floats (i.e. strings containing a decimal separator, '.').
To do so, simply replace any length calls above with
index($n.'.','.').
| Answer: How do I print a large integer with thousands separators? contributed by hanspoo $num = 123456789;
$num = reverse $num; # reverse the number's digits
$num =~ s/(\d{3})/$1,/g; # insert comma every 3 digits, from beginning
$num = reverse $num; # Reverse the result
$num =~ s/^\,//; # remove leading comma, if any
| Answer: How do I print a large integer with thousands separators? contributed by Anonymous Monk my $number = '$12345678.90'; # dollars
$_ = reverse $number;
s/(\d{3})(?=\d)(?!\d*\.)/$1,/g;
$number = reverse $_;
|
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|