Adding commas to numbers

KevinADC

Convert 1000000 into 1,000,000 using a perl regexp.

216 Views
About the Author
my $number - 1000000;
$number = commify($number);

sub commify {
   (my $num = shift) =~ s/\G(\d{1,3})(?=(?:\d\d\d)+(?:\.|$))/$1,/g; 
   return $num; 
}
KevinADC 192 Practically a Posting Shark

In the above code the first line should be:

my $number = 1000000;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.