Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How do I add commas to a number?

by vroom (His Eminence)
on Jan 18, 2000 at 22:54 UTC ( [id://2145]=perlquestion: print w/replies, xml ) Need Help??

vroom has asked for the wisdom of the Perl Monks concerning the following question: (numbers)

How do I add commas to a number?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I add commas to a number?
by Fastolfe (Vicar) on Nov 09, 2000 at 23:21 UTC
Answer: How do I add commas to a number?
by vroom (His Eminence) on Jan 18, 2000 at 22:58 UTC
    sub addcommas{ my ($text)=@_; $text=reverse $text; $text=~s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; $text=scalar reverse $text; return $text; }
      Very inefficient! Use:
      $commas=~ s/^(-?\d+)(\d{3})/$1,$2/;
      One line does it all.

      hawk@rthawk.com

        What happens if your value is 1_000_000 ?

        "Livet är hårt" sa bonden.
        "Grymt" sa grisen...

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How do I add commas to a number?
by ybiC (Prior) on Jul 14, 2002 at 12:19 UTC

    Adding just a wee tiny bit to vroom's and Fastolfe's answers...

    This ditty accepts a list of numbers for commification, like so:
        my($minC, $maxC, $medC) = commify($min, $max, $med);
    or so:
        my @numsC = commify(@nums);

    sub commify { my @output; for(@_){ my $input = $_; $input = reverse $input; $input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g; $input = reverse $input; push @output, $input; } return @output; }

    Originally posted as a Categorized Answer.

Re: How do I add commas to a number?
by Deven (Novice) on Feb 25, 2022 at 04:05 UTC
    A more recent addition to perlfaq5:
    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+? # 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;
Re: How do I add commas to a number?
by Roy Johnson (Monsignor) on Jun 16, 2004 at 21:35 UTC
    A lookaround method that will work on any numbers embedded in a string (but will not commify after a decimal point):
    sub commify { local $_ = shift; s{(?<!\d|\.)(\d{4,})} {my $n = $1; $n=~s/(?<=.)(?=(?:.{3})+$)/,/g; $n; }eg; return $_; }
    If you prefer the FAQ rule of only working on things that begin with digits or -,
    sub commify { local $_ = shift; s{(?:(?<=^)|(?<=^-))(\d{4,})} {my $n = $1; $n=~s/(?<=.)(?=(?:.{3})+$)/,/g; $n; }e; return $_; }
    This one does a single pattern match and then uses substr() to insert the commas:
    sub commify { local $_ = shift; if (/^-?(\d{4,})/g) { for (my $p=$+[1]; $p>$-[1]; $p-=3) { substr($_,$p,0) = ',' } } return $_; }
Re: How do I add commas to a number?
by Dodger (Novice) on Jan 21, 2015 at 22:35 UTC

    I know it's eons old but I just noticed this (when looking for if there was some easy one-liner for this); it's good that that was removed from that FAQ mentioned above in the first answer.

    Since return() casts in list context:

    return reverse $input;

    will not work (at least not as expected), as it reverses the list of the result, e.g. ('098,765,432,1') to ('098,765,432,1') (it's a list of one).

    What you'd need is:

    return scalar reverse $input;
Re: How do I add commas to a number?
by spadacciniweb (Curate) on Oct 06, 2007 at 12:26 UTC
    From perlfaq5:
    This regexp will add commas to numbers:
    s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2025-07-15 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.