Retourne la valeur arrondie de num
à la précision precision (nombre de
chiffres après la virgule). Le paramètre precision
peut être négatif ou null : c'est sa valeur par défaut.
Si le paramètre precision est positif,
num sera arrondi en utilisant le paramètre
precision pour définir le nombre significatif
de chiffres après le point décimal.
Si le paramètre precision est négatif,
num arrondi en utilisant le paramètre
precision pour définir le nombre significatif
de chiffres avant le point décimal, i.e. le multiple le plus proche
de pow(10, -$precision), i.e. pour une
precision de -1, num
sera arrondi à 10, pour une precision de -2 à 100, etc.
mode
Utilisez RoundingMode ou l'une des constantes suivantes pour spécifier la méthode d'arrondi.
Méthode d'arrondi avec 9.5
float(10)
float(9)
float(10)
float(9)
Méthode d'arrondi avec 8.5
float(9)
float(8)
float(8)
float(9)
Exemple #4 Exemple avec mode et precision
<?php echo "Utilisation de PHP_ROUND_HALF_UP avec une précision d'une décimale" . PHP_EOL; var_dump(round( 1.55, 1, PHP_ROUND_HALF_UP)); var_dump(round(-1.55, 1, PHP_ROUND_HALF_UP));
echo PHP_EOL; echo "Utilisation de PHP_ROUND_HALF_DOWN avec une précision d'une décimale" . PHP_EOL; var_dump(round( 1.55, 1, PHP_ROUND_HALF_DOWN)); var_dump(round(-1.55, 1, PHP_ROUND_HALF_DOWN));
echo PHP_EOL; echo "Utilisation de PHP_ROUND_HALF_EVEN avec une précision d'une décimale" . PHP_EOL; var_dump(round( 1.55, 1, PHP_ROUND_HALF_EVEN)); var_dump(round(-1.55, 1, PHP_ROUND_HALF_EVEN));
echo PHP_EOL; echo "Utilisation de PHP_ROUND_HALF_ODD avec une précision d'une décimale" . PHP_EOL; var_dump(round( 1.55, 1, PHP_ROUND_HALF_ODD)); var_dump(round(-1.55, 1, PHP_ROUND_HALF_ODD)); ?>
L'exemple ci-dessus va afficher :
Utilisation de PHP_ROUND_HALF_UP avec une précision d'une décimale
float(1.6)
float(-1.6)
Utilisation de PHP_ROUND_HALF_DOWN avec une précision d'une décimale
float(1.5)
float(-1.5)
Utilisation de PHP_ROUND_HALF_EVEN avec une précision d'une décimale
float(1.6)
float(-1.6)
Utilisation de PHP_ROUND_HALF_ODD avec une précision d'une décimale
float(1.5)
float(-1.5)
As PHP doesn't have a a native number truncate function, this is my solution - a function that can be usefull if you need truncate instead round a number.
<?php /** * Truncate a float number, example: <code>truncate(-1.49999, 2); // returns -1.49 * truncate(.49999, 3); // returns 0.499 * </code> * @param float $val Float number to be truncate * @param int f Number of precision * @return float */ function truncate($val, $f="0") { if(($p = strpos($val, '.')) !== false) { $val = floatval(substr($val, 0, $p + 1 + $f)); } return $val; } ?>
this function (as all mathematical operators) takes care of the setlocale setting, resulting in some weirdness when using the result where the english math notation is expected, as the printout of the result in a width: style attribute!
Here's a function to round to an arbitary number of significant digits. Don't confuse it with rounding to a negative precision - that counts back from the decimal point, this function counts forward from the Most Significant Digit.
round() will sometimes return E notation when rounding a float when the amount is small enough - see https://bugs.php.net/bug.php?id=44223 . Apparently it's a feature.
To work around this "feature" when converting to a string, surround your round statement with an sprintf: