<?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 )
?>
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 |
<?php
setlocale(LC_ALL, 'de-DE', 'de_DE');
$val01 = M_LN10;
echo $val01 . '<br><br>';
echo $val01 . '<br>with 0 decimal places => ';
echo round($val01, 0);
echo '<br><br>'. $val01 . '<br>with 3 decimal places => ';
echo round($val01, 3);
echo '<br><br>'. $val01 . '<br>with 7 decimal places => ';
echo round($val01, 7);
echo '<br><br>'. $val01 . '<br>with 9 decimal places => ';
echo round($val01, 9);
echo '<br><br>'. $val01 . '<br>with 11 decimal places => ';
echo round($val01, 11);
echo '<br><br>'. $val01 . '<br>with 13 decimal places => ';
echo round($val01, 13);
echo '<br><br>'. $val01 . '<br>with 14 decimal places => ';
echo round($val01, 14);
?>
<?php
setlocale(LC_ALL, 'de-DE', 'de_DE');
$val02 = 13.512233556678;
$pre02 = mt_rand(0, 13);
$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>';
}
}
?>