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

Add commas to numbers, a bit better

by fundflow (Chaplain)
on Mar 02, 2001 at 21:43 UTC ( [id://61829]=perlcraft: print w/replies, xml ) Need Help??

   1: #!/usr/bin/perl -ps
   2: # This one is a small improvement of the code that adds commas
   3: #   to numbers. It has the advantage of not breaking tabular
   4: #   format (such as the output of `ls` and `du`)
   5: # Adopted from Perl Monks "How do I add commas to a number?"
   6: #
   7: # Use -r switch to cancel the alignment
   8: #
   9: # Example: ls -l | ,
  10: 
  11: unless($r) { while (s/\s (?=\d)/ 0/g) {} }
  12: $_=reverse;
  13: s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
  14: $_=scalar reverse;
  15: unless($r) { while (s/\s[0,](?=[\d,])/  /g) {} }

Replies are listed 'Best First'.
Re: Add coma to numbers, a bit better
by japhy (Canon) on Mar 02, 2001 at 23:04 UTC

      japhy, am I missing something about why reverse regexen would be little known? The reverse commify thingie has been in perlfaq5 for about 4 years now.

        But the algorithm was never named, nor paid much attention to. That FAQ entry is an isolated use of its powerful abilities.

        japhy -- Perl and Regex Hacker
      Am i missing something here?

      This coma part of this script is exactly the one given in the FAQ BUT it adds two lines that make sure not to break tabular format --- which is all I wanted to share here.

      fun.

Re: Add coma to numbers, a bit better
by aardvark (Pilgrim) on Mar 04, 2001 at 02:05 UTC
    Have you given a look at Number::Format?
    Beside adding in commas to numbers; it allows you to round numbers to your precision, format prices, and a bunch of other fun formaty things.

    Get Strong Together!!

      I'm not sure that i follow, but if you have suggestions for improvements they are welcome.
Re: Add coma to numbers, a bit better
by buckaduck (Chaplain) on Mar 10, 2001 at 04:18 UTC
    I like this code; it works great for me.

    One drawback is that when I use it for "ls -l" output as suggested, the year is commified as well as the file size. (For my older files, obviously). This then tends to mess up the column alignment, which the author had intended to preserve.

    total 6,592 drwxr-xr-x 2 kromk apbr 118,784 Jun 29 1,998 DSC/ -rwxr-xr-x 1 kromk apbr 7,597 Oct 31 16:25 KEAVT.KTC* drwxr-xr-x 7 kromk apbr 4,096 Oct 17 1,999 archive/ drwxr-xr-x 6 kromk apbr 4,096 Aug 7 2,000 backup/ drwxr-xr-x 46 kromk apbr 8,192 Mar 7 14:03 build/ drwxr-xr-x 9 kromk apbr 16,384 Mar 8 17:28 download/

    Goes to show that you can't have everything...

    buckaduck

Re: Add commas to numbers, a bit better
by Deven (Novice) on Feb 25, 2022 at 04:06 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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (1)
As of 2024-04-25 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found