Rounding up numbers in PERL

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     ?
Europa MacDonaldChief slayer of dragonsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
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
Europa MacDonaldChief slayer of dragonsAuthor Commented:
using print floor $avg; gave me

456,657,432,345,677,899,322, floor 540.714285714286
0
Europa MacDonaldChief slayer of dragonsAuthor Commented:
written :

  print OUT "$n1,$n2,$n3,$n4,$n5,$n6,$n7, floor $avg\n";
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Suhas .Senior QA ManagerCommented:
print "%d",$avg gives the rounded value of an average result.
0
ozoCommented:
use POSIX;
print OUT "$n1,$n2,$n3,$n4,$n5,$n6,$n7,",floor $avg,"\n";
0
Europa MacDonaldChief slayer of dragonsAuthor Commented:
still getting 24,36,45,67,887,33,320, floor 201.285714285714
0
ozoCommented:
> 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
ozoCommented:
> still getting 24,36,45,67,887,33,320, floor 201.285714285714
do you still have the "floor" in  quotes?
0
ozoCommented:
print "$n1,$n2,$n3,$n4,$n5,$n6,$n7,",(floor $avg),"\n";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
or
 print "$n1,$n2,$n3,$n4,$n5,$n6,$n7,", floor($avg) , "\n";
0
Europa MacDonaldChief slayer of dragonsAuthor Commented:
thankyou :-)
0
Suhas .Senior QA ManagerCommented:
#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
Europa MacDonaldChief slayer of dragonsAuthor Commented:
thankyou Suhas
0
ozoCommented:
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
TintinCommented:
A useful feature to use is the perl FAQ.  From the command line type

perldoc -q floor

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.