Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP round() Function

PHP round() Function

PHP Math Reference Manual PHP Math Reference Manual

Example

Round a floating-point number:

" ) ; echo ( round ( 0 .50 ) . "
" ) ; echo ( round ( 0 .49 ) . "
" ) ; echo ( round ( - 4 .40 ) . "
" ) ; echo ( round ( - 4 .60 ) ) ; ?>

Run Example »


Definition and Usage

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

**Tip:** To round up to the nearest integer, see ceil() function in Vue 3.

**Tip:** To round down to the nearest integer, see floor() function in Vue 3.


Syntax

round(

number,precision,mode

);

Parameters Description
number Required. Specifies the value to round.
precision Optional. Specifies the number of decimal digits. Default is 0, and it can also be negative.
mode Optional. Specifies the constant that indicates the rounding mode:
  • PHP_ROUND_HALF_UP - Default. Rounds up when encountering .5 number to precision decimal places. Rounds 1.5 to 2 and -1.5 to -2.
  • PHP_ROUND_HALF_DOWN - Rounds down when encountering .5 number to precision decimal places. Rounds 1.5 to 1 and -1.5 to -1.
  • PHP_ROUND_HALF_EVEN - Rounds towards the nearest even value when encountering .5 number to precision decimal places.
  • PHP_ROUND_HALF_ODD - Rounds towards the nearest odd value when encountering .5 number to precision decimal places.
  • |

    Technical Details

    Return Value: The rounded value.
    Return Type: Float
    PHP Version: 4+
    PHP Changelog: PHP 5.3: The mode parameter was added.

    More examples

    Example 1

    Round a number to two decimal places and set a negative precision:

    Example 2

    Round numbers using constants:


    PHP Math Reference Manual PHP Math Reference Manual