Front page | perl.beginners |
Postings from April 2002
Re: formatting numbers
Thread Previous
From:
John W. Krahn
Date:
April 29, 2002 00:40
Subject:
Re: formatting numbers
Message ID:
3CCCF8FE.E3FE7D80@acm.org
Pat wrote:
>
> I have been through man printf, info printf and perldoc -f print and still
> can't find how to format numbers.
> In the program below I would like to print the numbers as below:
> 383.3 as 383.30
$ perl -le'printf "%.2f\n", 383.3'
383.30
> 37492908 as 37 492 908
If we slightly modify the commify() sub from perlfaq5
$ perl -e'
sub commify {
local $_ = shift;
1 while s/^([-+]?\d+)(\d{3})/$1 $2/;
return $_;
}
print commify( 37492908 ), "\n"'
37 492 908
> 35.7560234069824 as 35.76
$ perl -le'printf "%.2f\n", 35.7560234069824'
35.76
> 1631.11929000261 as 1 631
$ perl -e'
sub commify {
local $_ = shift;
1 while s/^([-+]?\d+)(\d{3})/$1 $2/;
return $_;
}
print commify( int( 1631.11929000261 ) ), "\n"'
1 631
John
--
use Perl;
program
fulfillment
Thread Previous