Ceil/Floor/Round in Perl
Ceil in Perl
Cieling is basically outputing the next largest integer for an given real number.
Code:
ceiling(x) is the smallest integer not less than x
Ways to do it in Perl, a) using our own logic
Code: Perl
my $num = 45.4; my $ceil = int($num + 0.99);
b) using POSIX
Code: Perl
use POSIX qw/ceil/;
my $num = 45.4; my $ceil = ceil($num);
Floor in Perl
Flooring is outputing the next smallest integer for an given real number.
Code:
floor(x) is the largest integer not greater than x
Ways to do it in Perl, a) using our own logic
Code: Perl
my $num = 45.4; my $floor = int($num);
b) using POSIX
Code: Perl
use POSIX qw/floor/;
my $num = 45.4; my $floor = floor($num);
Round in perl
Rounding is converting any floatingpoint number to the nearest integer.
Ways to do it in Perl, a) using our own logic
Code: Perl
my $num = 45.4; my $round = int($num + 0.5);
b) using Math::Round
Code: Perl
use Math::Round qw/round/;
my $num = 45.4; my $round = round($num);
It'll be really nice if everyone crontributes to this thread with the equivalent functions in the language they are comfortable with.
