Rounding Floating-Point Numbers

Problem

You want to round a floating-point value to a certain number of decimal places. This problem arises as a result of the same inaccuracies in representation that make testing for equality difficult (see Section 2.2), as well as in situations where you must reduce the precision of your answers for readability.

Solution

Use the Perl function sprintf, or printf if you’re just trying to produce output:

$rounded = sprintf("%FORMATf", $unrounded);

Discussion

Rounding can seriously affect some algorithms, so the rounding method used should be specified precisely. In sensitive applications like financial computations and thermonuclear missiles, prudent programmers will implement their own rounding function instead of relying on the programming language’s built-in logic, or lack thereof.

Usually, though, we can just use sprintf. The f format lets you specify a particular number of decimal places to round its argument to. Perl looks at the following digit, rounds up if it is 5 or greater, and rounds down otherwise.

$a = 0.255;
$b = sprintf("%.2f", $a);
print "Unrounded: $a\nRounded: $b\n";

printf "Unrounded: $a\nRounded: %.2f\n", $a;


                  Unrounded: 0.255
               
                  Rounded: 0.26
               
                  Unrounded: 0.255
               
                  Rounded: 0.26

Three functions that may be useful if you want to round a floating-point value to an integral value are int , ceil, and floor. int, built into Perl, returns the integral portion of the floating-point number passed to it (int will use $_ if it was called without an ...

Get Perl Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.