"); echo(round(0.50) . "
"); echo(round(0.49) . "
"); echo(round(-4.40) . "
"); echo(round(-4.60)); ?> ```"> "); echo(round(0.50) . "
"); echo(round(0.49) . "
"); echo(round(-4.40) . "
"); echo(round(-4.60)); ?> ```" />
Easy Tutorial
❮ Func Date Checkdate Func String Strncmp ❯

PHP round() Function

PHP Math Reference Manual

Example

Rounding floating-point numbers:

<?php
echo(round(0.60) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.49) . "<br>");
echo(round(-4.40) . "<br>");
echo(round(-4.60));
?>

Definition and Usage

The round() function rounds a floating-point number.

Note: To round up to the nearest integer, see the ceil() function.

Note: To round down to the nearest integer, see the floor() function.


Syntax

Parameter Description
number Required. Specifies the value to round.
precision Optional. Specifies the number of decimal places. Default is 0, can also be negative.
mode Optional. Specifies a constant representing the rounding mode: PHP_ROUND_HALF_UP - Default. Rounds number up to precision decimal places when .5 is encountered. Rounds 1.5 to 2, rounds -1.5 to -2.<br> PHP_ROUND_HALF_DOWN - Rounds number down to precision decimal places when .5 is encountered. Rounds 1.5 to 1, rounds -1.5 to -1.<br> PHP_ROUND_HALF_EVEN - Rounds number to the next even number to precision decimal places when .5 is encountered.<br> PHP_ROUND_HALF_ODD - Rounds number to the next odd number to precision decimal places when .5 is encountered.

Technical Details

Return Value: The rounded value.
Return Type: Float
--- ---
PHP Version: 4+
--- ---
PHP Changelog: PHP 5.3: Added the mode parameter.
--- ---

More Examples

Example 1

Rounding numbers to two decimal places, setting negative numbers:

<?php
echo round(3.4);         // 3
echo round(3.5);         // 4
echo round(3.6);         // 4
echo round(3.6, 0);      // 4
echo round(1.95583, 2);  // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2);    // 5.05
echo round(5.055, 2);    // 5.06
?>

Example 2

Rounding numbers using constants:

<?php
echo round(9.5, 0, PHP_ROUND_HALF_UP);   // 10
echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9
echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10
echo round(9.5, 0, PHP_ROUND_HALF_ODD);  // 9

echo round(8.5, 0, PHP_ROUND_HALF_UP);   // 9
echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8
echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8
echo round(8.5, 0, PHP_ROUND_HALF_ODD);  // 9
?>

❮ Func Date Checkdate Func String Strncmp ❯