develooper Front page | perl.beginners | Postings from April 2002

RE: formatting numbers

Thread Previous | Thread Next
From:
Timothy Johnson
Date:
April 28, 2002 23:23
Subject:
RE: formatting numbers
Message ID:
C0FD5BECE2F0C84EAA97D7300A500D50025813FB@SMILEY
 
I can't test these, but I think they will work.  Play around with sprintf a
little bit.  Don't forget to check out 'perldoc -f sprintf'.

383.3    as 383.30         
   $var = sprintf("%3.2f",$var)

37492908 as 37 492 908     
   if($var 1 =~ /(\d{2})(\d{3})(\d{3})/){
      $formattedvar = "$1 $2 $3";
   }else{
      print "data formatted incorrectly!\n";
   }

35.7560234069824 as 35.76
   $var = sprintf("%2.2f",$var);

1631.11929000261 as 1 631
   $intvar = int($var); #strip off the trailing decimal number
   $intvar =~ /(\d)(\d{3})/;#match for one digit followed by three
   $formattedvar = "$1 $2";#new var = result from first parentheses
                           #followed by a space and the second

-----Original Message-----
From: pat
To: beginners@perl.org
Sent: 4/28/02 7:03 AM
Subject: formatting numbers

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
	37492908 as 37 492 908
	35.7560234069824 as 35.76
	1631.11929000261 as 1 631
Please can someone put me right. Many thanks for introducing me to the
split
command/function it sure is a winner.
My simple program which prints unformatted is below
Many thanks Pat.
Program follows:-
#!/usr/bin/perl
#Program that needs formatting of numbers
$Tot_time="383.3";
$Grand_total="37492908";
$Tot_megsG = $Grand_total / 1024 /1024;
print "Grand Total   = $Grand_total\t";
print "$Tot_megsG  megabytes\n\n";
print "Total time    = $Tot_time in minutes \n\n";
$seconds = ($Tot_time * 60);
$avercps = ($Grand_total / $seconds);
print "Average CPS   = $avercps \n\n";
print "Program has ended....\n\n";


-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About