Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

round

Rounds a number to a given number of digits.

Available since: PHP 4

Syntax

round(int|float $num, int $precision = 0): float

Parameters

ParameterTypeDescription
$numint|floatThe number to round.
$precisionintHow many decimal places to keep. A negative value rounds to tens, hundreds and so on.

Return value

floatThe rounded number.

Examples

To two decimal places:

<?php
 
echo round(3.14159, 2);

Output:

3.14

A half is rounded away from zero:

<?php
 
echo round(2.5), "\n";
echo round(-2.5);

Output:

3
-3

Rounding to hundreds:

<?php
 
echo round(1234, -2);

Output:

1200

Notes

The result is always a float, even when rounding to a whole number. A third argument selects a different rounding mode.

See also

  • floor — Rounds a number down to the nearest integer.
  • intdiv — Performs integer division.

Related recipes

In the tutorial

  • Operators — Arithmetic, comparison and logical operators in PHP, and operator precedence.

Thanks for reading!

Donate to the site — we work hard for you.