Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: how to always round up in perl?

by philipbailey (Chaplain)
on Feb 25, 2011 at 09:10 UTC ( #890147=note: print w/ replies, xml ) Need Help??


in reply to how to always round up in perl?

Or if you do not want to import a mass of functions into your namespace from the POSIX module, just do this:

my $n = 3.3; my $ceiling = int($n + 1);

(Update) Above is incorrect, as pointed out by others. This works:

my $ceiling = ($n == int $n) ? $n : int($n + 1);


Comment on Re: how to always round up in perl?
Select or Download Code
Re^2: how to always round up in perl?
by Ratazong (Prior) on Feb 25, 2011 at 09:18 UTC
    Which I wouldn't recommend, because it fails in the following situation (with $n2) ...
    my $n1 = 3.3; my $n2 = 4; print int($n1 + 1)," ",int($n2 + 1), "\n";
Re^2: how to always round up in perl?
by chrestomanci (Priest) on Feb 25, 2011 at 09:19 UTC

    That fails if $n is allready an integer. You would need something like:

    my $n = 3.0; my $ceiling = int($n); $ceiling += 1 if $ceiling != $n;

    Or as a one liner:

    my $ceiling = int($n) + ($n != int($n));
Re^2: how to always round up in perl? (selective import)
by toolic (Chancellor) on Feb 25, 2011 at 14:25 UTC
    if you do not want to import a mass of functions into your namespace from the POSIX module
    ... just do this:
    use warnings; use strict; use POSIX qw(ceil); for (3.3, 3.6, 3003.3) { print $_, "\t", ceil($_), "\n"; } __END__ 3.3 4 3.6 4 3003.3 3004
    According to How to Import, this selectively imports only the ceil function.

    Alternately, you could import nothing and call the function using POSIX::ceil.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://890147]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-09-16 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (36 votes), past polls