in reply to Re: how to always round up in perl?
in thread how to always round up in perl?

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));

Replies are listed 'Best First'.
Re^3: how to always round up in perl?
by sbohning (Initiate) on Oct 11, 2021 at 17:01 UTC
    They also fail with negative non-integers.
    perl -MPOSIX=ceil -e "$n=-2.1; print($n, ' POSIX: ', ceil($n))" # -2.1 POSIX: -2 perl -e "$n=-2.1; print($n, ' other: ', int($n) + ($n != int($n)))" # -2.1 other: -1