Martin Moss wrote:
> All,
> 
> I need to do some number formatting, so I can shorten
> some rather large number strings.
> 
> 1000,000,000 would become 1000m
> 
> Does anybody know of a cpan module to do this. I had a
> look but couldn't find anything which did this.

use strict;
my @data = ('1000,000,000', '10,000,000', '1,000,000,000', '100000,000,000',
  '10000000,000,000', '1000000000,000,000', );

foreach $_ (@data) {
        print "$_\n";           # print orig
        s/,//g;                 # remove commas
        s/0{6,6}$/M/;           # change to megas
        $_ = commify ($_);      # fix commas
        print "$_\n";           # print result
}

sub commify ($) {               # $ret = commify ($number)
        local $_ = shift;

return $_ if tr/0-9/0-9/ < 4;
1 while s/^\s*([-+]?\d+)(\d{3})/$1,$2/;
return $_;

}

__END__



-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to