PHP round () function

PHP

Syntax:
float round ($number, $precision, $mode);
Parameters :It takes three parameters, of which one is required and two are optional. All of these options are described below:
  • $number : This is the number you want to round.
  • $precision: This is an optional parameter. Specifies the number of decimal digits to. The default value for this parameter is zero.
  • $mode : This parameter is optional. Specifies a constant to indicate the rounding mode. The constant can be one of the following:
    • PHP_ROUND_HALF_UP: this mode instructs to round the number specified by $number with the precision specified by $precision from zero .
    • PHP_ROUND_HALF_DOWN: This mode tells you to round the number specified by the $number, parameter with the precision specified by the $precision, parameter to zero.
    • PHP_ROUND_HALF_EVEN: This mode tells you to round the number specified by the $number, parameter with the precision specified by the $precision, parameter to the nearest even value.
    • PHP_ROUND_HALF_ODD: This mode instructs to round the number specified by the $number, parameter with the precision specified by the $precision, parameter to the nearest odd value.
  • Returned value:returns a rounded value.Examples:
    Input: round (0.70) Output: 1 Input: round (0.708782) Output: 0.71 Input: round (-3.40) Output: -3 Inp ut: round (-3.60) Output: -4
    The following programs illustrate how round() works in PHP:
    • When a parameter is passed with the default precision, that is, "0 ": echo ( round (0.70 ));  
      ?>
      Output:
      1
    • When a parameter is passed with a specific precision value: echo ( round (0.70878, 2));  
      ?>
      Output:
      0.71
    • When a negative value is passed as a parameter: echo ( round (- 3.40));  
      ?>
      Output:
      -3
    • Passing parameters in the mode:  
      // round to the nearest even value echo ( round (7.5,0, PHP_ROUND_HALF_EVEN)); echo "" ;  
      // round to the nearest odd value echo ( round (7.5,0, PHP_ROUND_HALF_ODD)); echo "" ;  
      // round to zero echo ( round ( 7.5,0, PHP_ROUND_HALF_DOWN)); echo "" ;  
      // round off from zero echo ( round ( 7.5,0, PHP_ROUND_HALF_UP));  
      ?>
      Output:
      8 7 7 8
    Important points to note:
    • The round() function is used to round numbers with floating point.
    • A specific precision value can be used to produce the desired results.
    • A precision value can also be negative or zero.
    Link :
    http://php.net/manual/en/function.round.php



    Get Solution for free from DataCamp guru