Rounding numbers in Perl

In the Perl programming language there are several ways how you can round a number to an integer.

Depending on what you need to in the end need to use different methods. When choosing the method of rounding should pay attention to how needs rounded negative numbers.

Here is a table which shows the results of different methods of rounding. All these methods for rounding are available in Perl out of the box, if you have Perl, you can use any of these methods.

                        -1.8  -1.5  -1.2   0     1.2   1.5   1.8
.................................................................
int($x)                 -1    -1    -1     0     1     1     1
POSIX::floor($x)        -2    -2    -2     0     1     1     1
int($x + 0.5)           -1    -1     0     0     1     2     2
sprintf('%.0f', $x)     -2    -2    -1     0     1     2     2
POSIX::ceil($x)         -1    -1    -1     0     2     2     2

(This table was created using code https://PerlBanjo.com/bf555d3112 ).

  • int($x) — the fractional part is discarded
  • POSIX::floor($x) — always rounds down
  • int($x + 0.5) — for positive numbers work on the rule of mathematical rounding, and negative numbers gives out some nonsense
  • sprintf('%.0f', $x) — works by the rule of mathematical rounding for positive and negative numbers.
  • POSIX::ceil($x) — always rounds up

Rounding to a few decimal places

One of the ways how to round a number to few decimal places in Perl to use it sprintf("%.2f", $x).

The number in %.2f means how much you need to leave the digits after the decimal point. Here is an example code:

▶ Run
#!/usr/bin/perl

use feature qw(say);
use Math::Trig;

say sprintf("%.0f", pi());
say sprintf("%.1f", pi());
say sprintf("%.2f", pi());
say sprintf("%.3f", pi());
say sprintf("%.4f", pi());
say sprintf("%.5f", pi());

Result:

3
3.1
3.14
3.142
3.1416
3.14159

Ways of rounding in newer versions of Perl

Starting in Perl 5.22 in the standard distribution of Perl features implemented POSIX::trunc() and POSIX::round(), and from Perl 5.24 the function of POSIX::lround(). Here is the table with result of all of the ways (https://PerlBanjo.com/4314405cde ):

                        -1.8  -1.5  -1.2   0     1.2   1.5   1.8
.................................................................
int($x)                 -1    -1    -1     0     1     1     1
POSIX::trunc($x)        -1    -1    -1     0     1     1     1
POSIX::floor($x)        -2    -2    -2     0     1     1     1
int($x + 0.5)           -1    -1     0     0     1     2     2
POSIX::round($x)        -2    -2    -1     0     1     2     2
POSIX::lround($x)       -2    -2    -1     0     1     2     2
sprintf('%.0f', $x)     -2    -2    -1     0     1     2     2
POSIX::ceil($x)         -1    -1    -1     0     2     2     2

Results POSIX::trunc($x) in this table are the same as int($x). And exactly the same results POSIX::round($x) and POSIX::lround($x) in this table exactly the same as sprintf('%.0f', $x).

Other articles