Solved

Rounding up numbers in PERL

Posted on 2009-07-07
15
345 Views
Last Modified: 2012-06-27
I have this code:

my $avg = ($n1 + $n2 + $n3 + $n4 + $n5 + $n6 + $n7)/ 7;

which gives me an average of all the values in those variables.
The result is this:
24,36,45,67,887,33,320, 201.285714285714, with 201.285714285714 being the variable

how do I round this number to no decimal places, so it would just be 201     ?
0
Comment
Question by:MichaelGlancy
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24792363
use POSIX:
#round up
print ceil $avg;
#round down
print floor $avg;
#round to nearest
print floor $avg+0.5;
#or
printf "%.f",$avg;
0
 

Author Comment

by:MichaelGlancy
ID: 24792383
using print floor $avg; gave me

456,657,432,345,677,899,322, floor 540.714285714286
0
 

Author Comment

by:MichaelGlancy
ID: 24792392
written :

  print OUT "$n1,$n2,$n3,$n4,$n5,$n6,$n7, floor $avg\n";
0
 
LVL 9

Expert Comment

by:Suhas .
ID: 24792399
print "%d",$avg gives the rounded value of an average result.
0
 
LVL 84

Expert Comment

by:ozo
ID: 24792402
use POSIX;
print OUT "$n1,$n2,$n3,$n4,$n5,$n6,$n7,",floor $avg,"\n";
0
 

Author Comment

by:MichaelGlancy
ID: 24792411
still getting 24,36,45,67,887,33,320, floor 201.285714285714
0
 
LVL 84

Expert Comment

by:ozo
ID: 24792427
> print "%d",$avg gives the rounded value of an average result.
no, it gives
%d540.714285714286
printf  "%d",$avg; #gives the truncated value of $avg
printf  "%.f",$avg; #gives the rounded value of $avg
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

by:ozo
ID: 24792439
> still getting 24,36,45,67,887,33,320, floor 201.285714285714
do you still have the "floor" in  quotes?
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 24792444
print "$n1,$n2,$n3,$n4,$n5,$n6,$n7,",(floor $avg),"\n";
0
 
LVL 84

Expert Comment

by:ozo
ID: 24792454
or
 print "$n1,$n2,$n3,$n4,$n5,$n6,$n7,", floor($avg) , "\n";
0
 

Author Closing Comment

by:MichaelGlancy
ID: 31600507
thankyou :-)
0
 
LVL 9

Expert Comment

by:Suhas .
ID: 24792466
#thanks for the correction for print to printf ozo.
# since Michael request is "how do I round this number to no decimal places, so it would just be 201"


Michael,
Is your requirement like 201.**** to 201, if so its better to use %d
or less than 201.5 to 201 and greather than 201.5 to 202 , then use %f with condition


Cheers...
Suhas
0
 

Author Comment

by:MichaelGlancy
ID: 24792492
thankyou Suhas
0
 
LVL 84

Expert Comment

by:ozo
ID: 24792510
the question title was about rounding up
but 201.285714285714 to 201 is rounding down
or it could be rounding to the nearest integer, or it could be truncating.
The difference would be seen with 201.9 and -201.9
0
 
LVL 48

Expert Comment

by:Tintin
ID: 24792785
A useful feature to use is the perl FAQ.  From the command line type

perldoc -q floor

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sort hash by values desc 2 186
pipe to sed or perl, please help 5 127
use google analytics code in perl script 2 64
Perl script to delete older files 6 72
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now