APM: Question: breaking up a numeric variable

Mike Stok mike at stok.co.uk
Thu Sep 26 15:23:14 CDT 2002


On Thu, 26 Sep 2002, Goldilox wrote:

> Is there a good way to take a numeric variable of undetermined length
> 
> $var=3423723482309234098;
> $var2=3325656;
> etc.
> and print it out with a comma appropriately placed at every third character
> slot from the end, i.e.
> 
> 3,423,723,482,309,234,098
> 3,325,656
> etc.

Have you tried

  perldoc -q commas

This is a good way of searching the perl FAQ on a modern perl 
installation.

Found in /usr/local/mike/lib/perl5/5.8.0/pod/perlfaq5.pod
       How can I output my numbers with commas added?

               This one from Benjamin Goldberg will do it for
               you:

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

               or written verbosely:

       [...]

It may not be the best way, but you should know about perldoc and the -q 
flag...

Hope this helps,

Mike

-- 
mike at stok.co.uk                    |           The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA 
mike at exegenix.com                  | Fingerprint      0570 71CD 6790 7C28 3D60
http://www.exegenix.com/           |                  75D2 9EC4 C1C0 0599 13DA




More information about the Austin mailing list