round()

ElMurimi's avatar
Published Sep 10, 2023
Contribute to Docs

The round() returns an integer or a float to the nearest whole number or to the specified decimal precision.

Syntax

$roundedNumber = round($number, $decimals, $roundMode);
  • $number: the value to be rounded.
  • $decimals: the number of decimals or precision required.
  • $roundMode: an argument that accepts four constants to determine the rounding mode that include PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN, PHP_ROUND_HALF_ODD.

Examples

The example below demonstrates the use of the round() function to convert a float into the nearest whole number.

<?php
$roundedNumber = round(99.75);
echo $roundedNumber;
?>

This will result in the following output:

100

The example below demonstrates the use of the round() function to convert a float to 2 decimal numbers.

<?php
$roundedNumber = round(99.754321, 2);
echo $roundedNumber;
?>

This will result in the following output:

99.75

Codebyte Example

The codebyte below uses the round() function to round off pi to 3 decimal places:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn PHP on Codecademy