round

PHP 4, PHP 5, PHP 7, PHP 8
round - Rounds a float

round( int|float$num, [int$precision = 0], [int$mode = PHP_ROUND_HALF_UP] ): float

Returns the rounded value of num to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).

Parameters

num

The value to round.

precision

The optional number of decimal digits to round to.

If the precision is positive, num is rounded to precision significant digits after the decimal point.

If the precision is negative, num is rounded to precision significant digits before the decimal point, i.e. to the nearest multiple of pow(10, -precision), e.g. for a precision of -1 num is rounded to tens, for a precision of -2 to hundreds, etc.

mode

Use one of the following constants to specify the mode in which rounding occurs.

Constants Description
PHP_ROUND_HALF_UP Rounds num away from zero when it is half way there, making 1.5 into 2 and -1.5 into -2.
PHP_ROUND_HALF_DOWN Rounds num towards zero when it is half way there, making 1.5 into 1 and -1.5 into -1.
PHP_ROUND_HALF_EVEN Rounds num towards the nearest even value when it is half way there, making both 1.5 and 2.5 into 2.
PHP_ROUND_HALF_ODD Rounds num towards the nearest odd value when it is half way there, making 1.5 into 1 and 2.5 into 3.

Return Values

The value rounded to the given precision as a float.

Changelog

Version Description
8.0.0 num no longer accepts internal objects which support numeric conversion.

Related Functions

Example of round

Show all examples for round

PHP Version:


Function round:

Mathematical Functions Functions

Most used PHP functions