PHP | round( ) Function

While dealing with problems which have values consisting of a very high number of decimal digits such as (121.76763527823) we often come up with a problem of rounding them up. Rounding them up manually can be a very time consuming and erroneous practice. In place of that, an inbuilt function of PHP i.e round() can be used.
The round() function in PHP is used to round a floating-point number. It can be used to define a specific precision value which rounds number according to that precision value.Precision can be also negative or zero.
The round() function in PHP has 3 parameters which are number, precision, and mode among which the latter two are optional parameters.The round() function returns the rounded value of the argument passed.

Syntax:

float round($number, $precision, $mode);

Parameters: It accepts three paramters out of which one is compulsory and two are optional. All of these parameters are described below:



  1. $number : It is the number which you want to round.
  2. $precision : It is an optional parameter. It specifies the number of decimal digits to round to. The default value of this parameter is zero.
  3. $mode : It is an optional parameter. It specifies a constant to specify the rounding mode. The constant can be one of the following:
    • PHP_ROUND_HALF_UP: This mode tells to round up the number specified by parameter $number by precision specified by parameter $precision away from zero.
    • PHP_ROUND_HALF_DOWN: This mode tells to round up the number specified by parameter $number by precision specified by parameter $precision towards zero.
    • PHP_ROUND_HALF_EVEN: This mode tells to round up the number specified by parameter $number by precision specified by parameter $precision towards nearest even value.
    • PHP_ROUND_HALF_ODD: This mode tells to round up the number specified by parameter $number by precision specified by parameter $precision towards nearest odd value.

Return Value: It returns the rounded value.

Examples:

Input : round(0.70)
Output : 1

Input : round(0.708782)
Output : 0.71

Input : round(-3.40)
Output : -3

Input : round(-3.60)
Output : -4

Below programs illustrate the working of round() in PHP:

  • When a parameter is passed with default precision i.e. ‘0’:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    1
  • When a parameter is passed with a specific precision value:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    0.71
  • When a negative value is passed as a parameter:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    echo(round(-3.40));
      
    ?>

    chevron_right

    
    

    Output:

    -3
  • Passing parameters with mode:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // round to nearest even value
    echo(round(7.5,0,PHP_ROUND_HALF_EVEN));
    echo "\n";
      
    // round to nearest odd value
    echo(round(7.5,0,PHP_ROUND_HALF_ODD));
    echo "\n";
      
    // round towards zero
    echo(round(7.5,0,PHP_ROUND_HALF_DOWN));
    echo "\n";
      
    // round away from zero
    echo(round(7.5,0,PHP_ROUND_HALF_UP));
      
    ?>

    chevron_right

    
    

    Output:

    8
    7
    7
    8
    

Important Points To Note:

  • round() function is used to round floating point numbers.
  • A specific precision value can be used to get the desired results.
  • Precision value can also be negative or zero.

Reference:
http://php.net/manual/en/function.round.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.