Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Separating big numbers with commas

by Syrkres (Sexton)
on Sep 04, 2001 at 23:52 UTC ( #110140=note: print w/replies, xml ) Need Help??


in reply to Separating big numbers with commas

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

Replies are listed 'Best First'.
Re^2: Separating big numbers with commas
by Anonymous Monk on Feb 18, 2015 at 22:07 UTC
    How can I embed that into an UNIX Korn shell script? By way of an example here is a perl epoch timestamp conversion used in one of my scripts. LASTUPDATE=$(perl -e '($ss, $mm, $hh, $DD, $MM, $YY) = localtime('${LASTUPDATE}'); printf "%02d\/%02d\/%04d\ %02d\:%02d", $MM +1 , $DD , $YY + 1900, $hh, $mm')
      How can I embed that into an UNIX Korn shell script?
      Can't you sprintf to a variable, comma-munge it, and then print that?
      $x = sprintf "%02d\/%02d\/%04d\ %02d\:%02d", $MM +1 , $DD , $YY + 1900 +, $hh, $mm; $x =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g; print $x;

      You could lose the interim variable on the way to print, but it's easier to follow as above.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        Clarification, as I'm just starting with Perl, how do a write that perl syntax into a korn shell command line executable. By way of example, the ${NUM} value is passed in, and the "perl -e" and/or "perl -e and printf" syntax will print it with comma separators. NUM="1234567890" echo "${NUM} should print with comma separators" perl -e ????????????????

      I'm not a ksh user, but this script seems to work for me.

      ===

      cat ~/.local/bin/commafy #!/usr/bin/perl -nl $_ =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g; print;

      ===

      My usage (to find out that a recent nix build used 18 GB of space):

      $ find /nix/store -cmin -150 | perl -nle '$sum+=-s "$_";END{print $sum +}' | commafy 18,025,136,505

      2018-12-16 Athanasius added code and paragraph tags

Re: Re: Separating big numbers with commas
by Sifmole (Chaplain) on Sep 05, 2001 at 00:01 UTC
    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

        Heh, add a couple more decimal places and it will fail (unless you consider commas after the decimal point to be allowable):

        #!/usr/bin/perl -w use strict; my $number='12345.67890'; # with commas, should be: 12,345.67890 print "N=$number\n"; $number =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g; print "N=$number\n";

        Output:

        N=12345.67890 N=12,345.67,890

        Looks funky to me.

        Okay.
        Firstly, your example which does not work either, is different from the first example. The first example was
        $number=1234567; # with commas, should be "1,234,567" $number =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g;
        Which will result in the string 1,2,3,4,5,6,7; Not quite what we were looking for.

        Your second example both extends the string and adds a decimal portion, and does not work either.

        $number='1234567890.01'; $number =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g;
        Resulting in 1,234,567,890.0,1; You will notice the comma in the decimal portion of the resulting string. I find it strange the the output you presented in your message is correct. Did you paste it from a run, or transcribe it and introduce the alteration in the result by accident? As pointed out by Anon, if you introduce a few more decimal places you will end up with more commas in the decimal portion.
        $number='1234567890.0123456789'; # with commas, should be "1,234,567,8 +90.01 $number =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g;
        Resulting in 1,234,567,890.0,1,2,3,4,5,6,7,8,9.

        The problem comes in because we are testing against \D|$ to determine when to place to commas. This allows commas to be placed in the decimal portion. I don't have a great solution but you can do something like the following:

        $number='1234567890'; # with commas, should be "1,234,567,890.01 $number .= '.' if ($number !~ /\./); # Make sure we end with a decimal + if we don't have a decimal $number =~ s/(\d)(?=(\d{3},?)+(\.))/$1\,/g; chop $number if ($number =~ /\.$/); # Get rid of that useless trailing + decimal print "N $number \n"; $number='1234567890.01234567687'; # with commas, should be "1,234,567, +890.01 $number .= '.' if ($number !~ /\./);# Make sure we end with a decimal +if we don't have a decimal $number =~ s/(\d)(?=(\d{3},?)+(\.))/$1\,/g; chop $number if ($number =~ /\.$/); # Get rid of that useless trailing + decimal print "N $number \n";
        The are better solutions presented, I just wanted to respond to why this example did not work.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2020-06-07 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (41 votes). Check out past polls.

    Notices?