in reply to rounding to nearest integer

There's an entry in perlfaq4, which comes with perl (try `perldoc perlfaq4` or `perldoc -q round` at the command line prompt): "Does Perl have a round() function? What about ceil() and floor()? Trig functions?" which says to use sprintf. Note that for rounding to the nearest integer, you must use
sprintf "%.0f", $n
not
sprintf "%d", $n
The latter "rounds towards zero" (AKA "truncate"), just like int does.

It seems strange to me that POSIX (a standard module) implements ceil() and floor() and zillions of (mathematical and other) functions, doesn't seem to include a round() function.

Replies are listed 'Best First'.
Re^2: rounding to nearest integer
by Anonymous Monk on Feb 03, 2020 at 01:42 UTC
    It seems strange to me that POSIX (a standard module) implements ceil() and floor() and zillions of (mathematical and other) functions, doesn't seem to include a round() function.

    There are very many different forms of rounding.