Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Separating big numbers with commas

by Legg83 (Novice)
on Sep 04, 2001 at 23:37 UTC ( #110137=perlquestion: print w/ replies, xml ) Need Help??
Legg83 has asked for the wisdom of the Perl Monks concerning the following question:

How could I take a big number, and separate it with commas.. like

987654321 = 987,654,321

regardless of how large the number is, i always want to place a comma every three numbers to the left.

Comment on Separating big numbers with commas
Re: Separating big numbers with commas
by monkeygirl (Pilgrim) on Sep 04, 2001 at 23:44 UTC

    The Perl Cookbook (Recipe 2.17) has just what you need:

    my $number = 987654321; print commify($number); sub commify { my $text = reverse $_[0]; $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; return scalar reverse $text }

    Update: blakem is right. And besides, my answer is incomplete. For a more complete answer, look here.


    Sarah
    If Bill Gates can name a company after his "bedroom" problems, I can have a stupid sig that points it out.
      nice one.
Re: Separating big numbers with commas
by blakem (Monsignor) on Sep 04, 2001 at 23:44 UTC
    Try searching for commas in the search bar above... I think the answer has been covered several times before.

    -Blake

Re: Separating big numbers with commas
by Syrkres (Sexton) on Sep 04, 2001 at 23:52 UTC
    Woohoo!

    I can help for once! As I found previously!

    $number=1234567; # with commas, should be "1,234,567" $number =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g;
    Node:
    http://www.perlmonks.com/index.pl?node_id=28331&lastnode_id=864
      Uh, this doesn't work.
        Looks fine to me....
        #!/usr/bin/perl -wT use strict; my $number='1234567890.01'; # with commas, should be "1,234,567,890.01 +" print "N=$number\n"; $number =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g; print "N=$number\n";
        Output
        N=1234567890.01 N=1,234,567,890.01
        Care to elaborate on which part doesn't work?

        -Blake

Re: Separating big numbers with commas
by John M. Dlugosz (Monsignor) on Sep 05, 2001 at 01:33 UTC
    Don't forget to use the current locale information to determine what the correct separator character is and what positions to group it with.

    In fact, perllocale has an example of doing exactly that!example program that rewrites its command-line parameters as integers correctly formatted in the current locale.

    require 5.004; use POSIX qw(locale_h); # Get some of locale's numeric formatting parameters my ($thousands_sep, $grouping) = @{localeconv()}{'thousands_sep', 'grouping'}; # Apply defaults if values are missing $thousands_sep = ',' unless $thousands_sep; # grouping and mon_grouping are packed lists # of small integers (characters) telling the # grouping (thousand_seps and mon_thousand_seps # being the group dividers) of numbers and # monetary quantities. The integers' meanings: # 255 means no more grouping, 0 means repeat # the previous grouping, 1-254 means use that # as the current grouping. Grouping goes from # right to left (low to high digits). In the # below we cheat slightly by never using anything # else than the first grouping (whatever that is). if ($grouping) { @grouping = unpack("C*", $grouping); } else { @grouping = (3); } # Format command line params for current locale for (@ARGV) { $_ = int; # Chop non-integer part 1 while s/(\d)(\d{$grouping[0]}($|$thousands_sep))/$1$thousands_se +p$2/; print "$_"; } print "\n";
Re: Separating big numbers with commas (boo)
by boo_radley (Parson) on Sep 05, 2001 at 22:50 UTC
    update I'm not that special actually. Confession upcoming.

    use Number::Format; my $uuooop = new Number::Format; print $uuooop->format_number("1234567890.0123456789",2);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://110137]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-09-20 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls