The round Function

The round function rounds a number according to the rules of mathematical rounding. The first parameter accepts a number, and the second optional parameter specifies how many digits to leave in the fractional part. The second parameter can be either positive or negative (in which case it specifies how many digits to leave in the integer part).

Syntax

phpround(number, [precision]);

Example

Let's round a fraction to a whole number:

php<?php
  echo round(3.4);
?>

Code execution result:

php3

Example

Let's round a fraction to a whole number:

php<?php
  echo round(3.5);
?>

Code execution result:

php4

Example

Let's round a fraction to a whole number:

php<?php
  echo round(3.6);
?>

Code execution result:

php4

Example

Let's round a number to two digits in the fractional part:

php<?php
  echo round(12.45678, 2);
?>

Code execution result:

php12.46

Example

Let's round a number to three digits in the fractional part:

php<?php
  echo round(12.45678, 3);
?>

Code execution result:

php12.457

Example

Let's round a number to tens:

php<?php
  echo round(12456.78, -1);
?>

Code execution result:

php12460

Example

Let's round a number to hundreds:

php<?php
  echo round(12456.78, -2);
?>

Code execution result:

php12500

Example

Let's round a number to thousands:

php<?php
  echo round(12456.78, -3);
?>

Code execution result:

php12000

See Also

  • the ceil function,
    which rounds a fraction up
  • the floor function,
    which rounds a fraction down