RE: How to add commas to a number?
- From: John.Moon@xxxxxxxxxxxxxxxxx (John Moon)
- Date: Wed, 28 Nov 2007 16:22:18 -0500
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>
.
- References:
- How to add commas to a number?
- From: JerryP
- How to add commas to a number?
- Prev by Date: Re: Comparing Regular Expression in Perl vs Python
- Next by Date: Re: fixed list combinatorics
- Previous by thread: How to add commas to a number?
- Next by thread: Re: How to add commas to a number?
- Index(es):
Relevant Pages
|