http://qs321.pair.com?node_id=66948
Contributed by MrNobo1024 on Mar 25, 2001 at 08:03 UTC
Q&A  > math

#### Description:

int(\$num) rounds a number down, but there dosen't seem to be any function to round a number up. int(\$num + 1) works ok for fractional values, but dosen't work right for integers. How can I round numbers up in a way that works for all numbers?

 Answer: How do I round up a number?contributed by Anonymous Monk Use the POSIX module: ```use POSIX qw(ceil); print ceil(3.5); # prints 4 print ceil(4); # also prints 4 [download]``` or, you can use this function: ```sub roundup { my \$n = shift; return((\$n == int(\$n)) ? \$n : int(\$n + 1)) } [download]``` Answer: How do I round up a number?contributed by damian1301 Heh, go check out the Math::Round module...probably more efficient. Answer: How do I round up a number?contributed by powerman See all round-like perl functions here. Answer: How do I round up a number?contributed by Tiefling My preferred answer if you insist on rounding up is the second one given by Anonymous Monk above. However, it's best to round to the nearest integer, in which case int(\$n+0.5) is your friend. Tiefling