English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The round() function in PHP

Thisround()The function rounds a floating-point number. For example 0.90 to1,0.35to 0 and so on.

syntax

round(val, precision, mode)

parameter

  • val-the value to be rounded

  • precision-it sets the precision, that is, the number of decimal places to round to

  • mode-specifies the following rounding mode constants

    • PHP_ROUND_HALF_UP-the constant rounds val to a decimal precision (when it is in the middle). The1.5rounds to2, and-1.5rounds to-2. Default

    • PHP_ROUND_HALF_DOWN-the constant rounds val to one decimal place after the decimal point. The1.5rounds to1and-1.5rounds to-1

    • PHP_ROUND_HALF_EVEN-rounds val to the next even decimal place precision

    • PHP_ROUND_HALF_ODD-rounds val to the next odd decimal place precision.

returns

Thisround()The function returns the rounded value.

Example

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

Output Result

2.1

Example

Let's see another example-

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

Output Result

9.86

Example

Let's see another example-

<?php
   echo(round(10.5,0,PHP_ROUND_HALF_UP)  .  "<br>");
   echo(round(-10.5,0,PHP_ROUND_HALF_UP)  );
?>

Output Result

11<br>-11

Example

Let's see another example-

<?php
   echo(round(19.5,0,PHP_ROUND_HALF_DOWN)  .  "<br>");
   echo(round(-19.5,0,PHP_ROUND_HALF_DOWN)  .  "<br>");
?>

Output Result

19<br>-19<br>

Example

Let's see another example-

<?php
   echo(round(9.9,0,PHP_ROUND_HALF_EVEN)  .  "<br>");
   echo(round(-9.8,0,PHP_ROUND_HALF_EVEN)  .  "<br>");
   echo(round(11.8,0,PHP_ROUND_HALF_ODD)  .  "<br>");
   echo(round(-11.8,0,PHP_ROUND_HALF_ODD));
?>

Output Result

10<br>-10<br>12<br>-12