Recipe 2.3. Rounding Floating-Point Numbers
Author: Tom Christiansen & Nathan Torkington
Date: 1999-07-02T01:30:04Z

#

| Chapter 2 Numbers |

## 2.3. 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 Recipe 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 argument). The POSIX module's `floor` and `ceil` functions round their argument down and up to the next integer, respectively.

    use POSIX;
    print "number\tint\tfloor\tceil\n";
    @a = ( 3.3 , 3.5 , 3.7, -3.3 );
    foreach (@a) {
        printf( "%.1f\t%.1f\t%.1f\t%.1f\n",
            $_, int($_), floor($_), ceil($_) );
    }

    number  int     floor   ceil
     3.3     3.0     3.0     4.0
     3.5     3.0     3.0     4.0
     3.7     3.0     3.0     4.0
    -3.3    -3.0    -4.0    -3.0

### See Also

The `sprintf` and `int` functions in perlfunc (1) and Chapter 3 of Programming Perl; the `floor` and `ceil` entries in the documentation for the standard POSIX module (also in Chapter 7 of Programming Perl); we introduced the `sprintf` technique in Recipe 2.2

2.2. Comparing Floating-Point Numbers |  | 2.4. Converting Between Binary and Decimal