split
am 13.09.2004 00:12:03 von George Bouras# I want to separate numbers by thousands. I use this
$val = reverse $val;
$val =~s/(\d{3})/$1\./g;
$val = reverse $val;
$val =~s/^\.*//;
# Is there any shorter way ?
# I want to separate numbers by thousands. I use this
$val = reverse $val;
$val =~s/(\d{3})/$1\./g;
$val = reverse $val;
$val =~s/^\.*//;
# Is there any shorter way ?
George Bouras wrote:
> # I want to separate numbers by thousands. I use this
> $val = reverse $val;
> $val =~s/(\d{3})/$1\./g;
> $val = reverse $val;
> $val =~s/^\.*//;
> # Is there any shorter way ?
I'm using this:
1 while $val =~ s/(\d)(\d\d\d)(?!\d)/$1.$2/g;
Picked it up once from the FAQ, I think, but don't find it there now.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
"Gunnar Hjalmarsson"
news:2qk5jhFvu0o3U1@uni-berlin.de...
> George Bouras wrote:
> > # I want to separate numbers by thousands. I use this
> > $val = reverse $val;
> > $val =~s/(\d{3})/$1\./g;
> > $val = reverse $val;
> > $val =~s/^\.*//;
> > # Is there any shorter way ?
>
> I'm using this:
>
> 1 while $val =~ s/(\d)(\d\d\d)(?!\d)/$1.$2/g;
>
> Picked it up once from the FAQ, I think, but don't find it there now.
perldoc -q comma
How can I output my numbers with commas added?
This subroutine will add commas to your number:
sub commify {
local $_ = shift;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
}
That's from Perl 5.8.0
This regex will add commas no matter how long the number string is:
$val = 1000000000;
$val =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
This returns: 1,000,000,000
------------------------------------------------------------ --------------------------
"Tintin"
news:2ql6f0FvnutnU1@uni-berlin.de...
>
> "Gunnar Hjalmarsson"
> news:2qk5jhFvu0o3U1@uni-berlin.de...
>> George Bouras wrote:
>> > # I want to separate numbers by thousands. I use this
>> > $val = reverse $val;
>> > $val =~s/(\d{3})/$1\./g;
>> > $val = reverse $val;
>> > $val =~s/^\.*//;
>> > # Is there any shorter way ?
>>
>> I'm using this:
>>
>> 1 while $val =~ s/(\d)(\d\d\d)(?!\d)/$1.$2/g;
>>
>> Picked it up once from the FAQ, I think, but don't find it there now.
>
> perldoc -q comma
>
> How can I output my numbers with commas added?
>
> This subroutine will add commas to your number:
>
> sub commify {
> local $_ = shift;
> 1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
> return $_;
> }
>
> That's from Perl 5.8.0
>
>