Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thousands...

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi,

Mmm.. couldn't find anything via the "search" feature (no way really to narrow down to just one specific forum.. which seems a bit basic ;/).

Anyway .. does anyone have some code to hand, that will format thousand digits? i.e

12345667

..would become;

12,345,667

I've searched high and low for this (guess I'm probably not using the right search terms, as this must have been asked before <G>).

TIA

Andy
 
I didn't write this, just found at a valuable resource:
Code:
#!/usr/bin/perl
use strict;
use warnings;
sub Commify {
  my $text = reverse $_[0];
  $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
  return scalar reverse $text;
} # Commify
my $num = 12345667;
my $d = Commify($num);
print $d;
 
And if you don't like regular expressions you can also do this:

Code:
sub commify {
  my $text = reverse $_[0];
  my @digits = split('', $text);
  my $ct = 0;
  my $result = "";
  foreach my $num (@digits)
  {
    if($ct == 3)
    {
      $result = "," . $result;
      $ct = 0;
    }
    $result = $num . $result;
    $ct++;
  }
  return $result
}
my $num = 1234567;
my $d = commify($num);
print "$d\n";

But why anyone would do it that way is beyond me :)
 
hehheh.. thanks for the responses guys... very much appreciated :) I'll probably go with the non-regex version, but because the number we use could be pretty high/low .. i.e anything from 1 to 10,000,000 <G>

Thanks again.

Andy
 
You are free to choose your own poison. But, fair warning to you, the "non-regex" version will bugger any number that has a decimal in it-- 1000.01, 1.101, etc. The regex implementation is also about 2-4 times more effecient (benchmarked).

jaa
 
Hi Andy

10 million might be a big number... but it only has 8 digits - i can't imagine a regex is going to have much of a problem with that - after all it's only 2 commas. The regex doesn't think of it as a large number - just characters

I would use the regex version supplied by Xaqte if i were you


Kind Regards
Duncan
 
I'd use the regexp version too - it's source is the Perl Cookbook, AFAIK. Great resource.
 
Hi,

Thanks for the replies guys. However, I'll probably keep with the non-regex version, as it seems to work weLl thus FAR (only has about 100 numbers to format).

Thanks for the advice though =)

Cheers

Andy
 
If you're stuck on not using the regex version, you might try something like this - it handles decimals, and negative numbers. It also seems to benchmark marginally better than the regex version with a scalar argument. You can call it with lists instead of scalars, which saves on the overhead of multiple sub calls.

Code:
my @numbers = (-123456.78, 98764, 7683.12, 1.01, 500);

sub addCommas {
    my @nums = @_;
    @nums = map {
        my $neg = $_ < 0 ? '-' : '';
        my ($int,$dec) = (abs(int($_)), '');
        if (abs($_) != $int) {
            $dec = substr($_,length($int)+length($neg),(length($_) - length($int)));
        }
        my @temp = reverse split '', $int;
        for (my $i = 0; ($#temp - $i) > 2; $i++) {
            $i+=3; splice(@temp, $i, 0, ',');
        }
        $neg . reverse(@temp) . $dec;
    } @nums;
    return wantarray ? @nums : $nums[0];
}

local $, = "\n";
print addCommas(@numbers);
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top