round


php128 apg

ROUND a FLOAT value, whether or not establishing criteria for such.

Compatible with LOCALE only, up to PHP 7.4.XX.





This function returns the $num the given $precision as a FLOAT.



<?php

float round 
int|float $num
                             
int $precision 0
                             
int $mode PHP_ROUND_HALF_UP )


where,

$num The numeric value to round

$precision 
The number of decimal digits to round to

$mode 
The constant to specify the mode which rounding occurs
              
SEE the below TABLE )

?> 

$num


The number to round.

As off PHP 8.0.0 no longer accepts internal objects which support numeric conversion.



$precision


The optional number of decimal digits to round to.



$mode


One of the following constants:


CONSTANT VALUE MEANING DEFULT
PHP_ROUND_HALF_UP 1 The integer part will be rounded up if the first digit of the decimal part is 5 PHP_ROUND_HALF_UP
PHP_ROUND_HALF_DOWN 2 The integer part will be rounded down if the first digit of the decimal part is 5
PHP_ROUND_HALF_EVEN 3 Round $val to $precision decimal places towards the nearest even value.
PHP_ROUND_HALF_ODD 4 Round $val to $precision decimal places towards the nearest odd value.
ed48

  1 EXERCISE   

<?php

setlocale
(LC_ALL'de-DE''de_DE');

$val01 M_LN10;

echo 
$val01 '<br><br>';

echo 
$val01 '<br>with 0 decimal places => ';
echo 
round($val010);
echo 
'<br><br>'$val01 '<br>with 3 decimal places => ';
echo 
round($val013);
echo 
'<br><br>'$val01 '<br>with 7 decimal places => ';
echo 
round($val017);
echo 
'<br><br>'$val01 '<br>with 9 decimal places => ';
echo 
round($val019);
echo 
'<br><br>'$val01 '<br>with 11 decimal places => ';
echo 
round($val0111);
echo 
'<br><br>'$val01 '<br>with 13 decimal places => ';
echo 
round($val0113);
echo 
'<br><br>'$val01 '<br>with 14 decimal places => ';
echo 
round($val0114);

?> 

  2 EXERCISE   

<?php

setlocale
(LC_ALL'de-DE''de_DE');

$val02 13.512233556678;

$pre02 mt_rand(013);

$modes = ['PHP_ROUND_HALF_UP' => PHP_ROUND_HALF_UP,
 
'PHP_ROUND_HALF_DOWN' => PHP_ROUND_HALF_DOWN
 
'PHP_ROUND_HALF_EVEN' => PHP_ROUND_HALF_EVEN
 
'PHP_ROUND_HALF_ODD' => PHP_ROUND_HALF_ODD ];

foreach(
$modes as $mod => $md)
{
    for(
$a 0$a $pre02$a++)
    {

echo 
$mod '<br>precision = ' $a '<br>' 
                   
round($val02$a$md) . '<br><br>';

    }
}    

?>