RE: How to add commas to a number?



Subject: How to add commas to a number?

Hi!

I am trying to get this to work, but I am not having any luck?

use strict;
use warnings;

sub commify {
local($_) = shift;
1 while s/^(-?\d+)(\d{3})/$1,$2/;
return $_;
}

If I input these numbers:

6238.12
121150.98
104903.30
113569.26
40239.51
8733.55

I get :

6238.12
121150.98
104903.30
113569.26
40,239.51
8,733.55

Any ideas?

Thanks

Jerry


Does this help?
Jwm

SUN1-BATCH>./foo
6238.12=6,238.12
121150.98=121,150.98
104903.30=104,903.30
113569.26=113,569.26
40239.51=40,239.51
8733.55=8,733.55
SUN1-BATCH>more foo

#! /usr/local/bin/perl
use strict;

while (<DATA>) {
chomp;
print "$_" , '=' , commify($_), "\n";
}
sub commify {
local($_) = shift;
1 while s/^(-?\d+)(\d{3})/$1,$2/;
return $_;
}
__DATA__
6238.12
121150.98
104903.30
113569.26
40239.51
8733.55
SUN1-BATCH>
.



Relevant Pages