Click here to get back home

FAQ 5.13 How can I output my numbers with commas added?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 5.13 How can I output my numbers with commas added? PerlFAQ Server 06-10-2008
Posted by PerlFAQ Server on June 10, 2008, 3:03 am
Please log in for more thread options
This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

5.13: How can I output my numbers with commas added?


(contributed by brian d foy and Benjamin Goldberg)

You can use Number::Format to separate places in a number. It handles
locale information for those of you who want to insert full stops
instead (or anything else that they want to use, really).

This subroutine will add commas to your number:

sub commify {
local $_ = shift;
1 while s/^([-+]?\d+)(\d)/$1,$2/;
return $_;
}

This regex from Benjamin Goldberg will add commas to numbers:

s/(^[-+]?\d+?(?=(?>(?:\d)+)(?!\d))|\G\d(?=\d))/$1,/g;

It is easier to see with comments:

s/(
^[-+]? # beginning of number.
\d+? # first digits before first comma
(?= # followed by, (but not included in the
match) :
(?>(?:\d)+) # some positive multiple of three
digits.
(?!\d) # an *exact* multiple, not x * 3 + 1
or whatever.
)
| # or:
\G\d # after the last group, get three digits
(?=\d) # but they have to have more digits after
them.
)/$1,/xg;



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Posted by Mario D'Alessio on June 10, 2008, 11:15 am
Please log in for more thread options
Rather than using a complex RE, I reverse the
string and then just add a comma after each set
of 3 numbers:

$s = reverse $s;
$s =~ s/(\d)/,/g;
$s =~ s/,$//;
reverse $s;

Besides the fact that my code ignores the plus
and minus signs and the decimal point, is there any
other advantage to using the REs below? I'm guessing
that my two uses of "reverse" and the simple RE would
be more efficient than the complex REs below.

Just wondering...

Mario

> This is an excerpt from the latest version perlfaq5.pod, which
> comes with the standard Perl distribution. These postings aim to
> reduce the number of repeated questions as well as allow the community
> to review and update the answers. The latest version of the complete
> perlfaq is at http://faq.perl.org .
>
> --------------------------------------------------------------------
>
> 5.13: How can I output my numbers with commas added?
>
>
> (contributed by brian d foy and Benjamin Goldberg)
>
> You can use Number::Format to separate places in a number. It handles
> locale information for those of you who want to insert full stops
> instead (or anything else that they want to use, really).
>
> This subroutine will add commas to your number:
>
> sub commify {
> local $_ = shift;
> 1 while s/^([-+]?\d+)(\d)/$1,$2/;
> return $_;
> }
>
> This regex from Benjamin Goldberg will add commas to numbers:
>
> s/(^[-+]?\d+?(?=(?>(?:\d)+)(?!\d))|\G\d(?=\d))/$1,/g;
>
> It is easier to see with comments:
>
> s/(
> ^[-+]? # beginning of number.
> \d+? # first digits before first comma
> (?= # followed by, (but not included in
> the match) :
> (?>(?:\d)+) # some positive multiple of
> three digits.
> (?!\d) # an *exact* multiple, not x * 3
> + 1 or whatever.
> )
> | # or:
> \G\d # after the last group, get three
> digits
> (?=\d) # but they have to have more digits
> after them.
> )/$1,/xg;
>
>
>
> --------------------------------------------------------------------
>
> The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
> are not necessarily experts in every domain where Perl might show up,
> so please include as much information as possible and relevant in any
> corrections. The perlfaq-workers also don't have access to every
> operating system or platform, so please include relevant details for
> corrections to examples that do not work on particular platforms.
> Working code is greatly appreciated.
>
> If you'd like to help maintain the perlfaq, see the details in
> perlfaq.pod.



Posted by brian d foy on June 11, 2008, 8:49 am
Please log in for more thread options

> Rather than using a complex RE, I reverse the
> string and then just add a comma after each set
> of 3 numbers:

Try it with 1234567.1234567 :)

Posted by Mario D'Alessio on June 11, 2008, 11:46 pm
Please log in for more thread options

>
>> Rather than using a complex RE, I reverse the
>> string and then just add a comma after each set
>> of 3 numbers:
>
> Try it with 1234567.1234567 :)

Note that I said "Besides the fact that my code ignores
the plus and minus signs and the decimal point..."

I use the code only in places where I know I'm
getting whole numbers (e.g. reading byte sizes
of files).

Mario



Similar ThreadsPosted
FAQ 5.11: How can I output my numbers with commas added? October 26, 2004, 5:03 am
FAQ 5.11 How can I output my numbers with commas added? January 19, 2005, 6:03 am
FAQ 5.12 How can I output my numbers with commas added? April 13, 2005, 5:03 am
FAQ 5.12 How can I output my numbers with commas added? June 28, 2005, 10:03 pm
FAQ 5.12 How can I output my numbers with commas added? October 22, 2005, 10:03 am
FAQ 5.12 How can I output my numbers with commas added? December 28, 2005, 11:03 pm
FAQ 5.12 How can I output my numbers with commas added? February 3, 2006, 12:03 pm
FAQ 5.12 How can I output my numbers with commas added? April 25, 2006, 9:03 am
FAQ 5.12 How can I output my numbers with commas added? July 1, 2006, 9:03 am
FAQ 5.12 How can I output my numbers with commas added? July 27, 2006, 3:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap