Fonction PHP round()
Exemple
Chiffres ronds :
<?php
echo(round(0.60) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.49) . "<br>");
echo(round(-4.40) . "<br>");
echo(round(-4.60));
?>
Définition et utilisation
La fonction round() arrondit un nombre à virgule flottante.
Astuce : Pour arrondir un nombre à l'entier supérieur le plus proche, regardez la fonction ceil() .
Astuce : Pour arrondir un nombre à l'entier le plus proche, regardez la fonction floor() .
Syntaxe
round(number,precision,mode);
Valeurs des paramètres
Parameter | Description |
---|---|
number | Required. Specifies the value to round |
precision | Optional. Specifies the number of decimal digits to round to. Default is 0 |
mode | Optional. Specifies a constant to specify the rounding mode:
|
Détails techniques
Valeur de retour : | La valeur arrondie |
---|---|
Type de retour : | Flotter |
Version PHP : | 4+ |
Journal des modifications PHP : | PHP 5.3 : Le paramètre mode a été ajouté |
Plus d'exemples
Exemple
Arrondissez les nombres à deux décimales :
<?php
echo(round(4.96754,2) . "<br>");
echo(round(7.045,2) . "<br>");
echo(round(7.055,2));
?>
Exemple
Arrondir les nombres à l'aide des constantes :
<?php
echo(round(1.5,0,PHP_ROUND_HALF_UP) . "<br>");
echo(round(-1.5,0,PHP_ROUND_HALF_UP) . "<br>");
echo(round(1.5,0,PHP_ROUND_HALF_DOWN) . "<br>");
echo(round(-1.5,0,PHP_ROUND_HALF_DOWN) . "<br>");
echo(round(1.5,0,PHP_ROUND_HALF_EVEN) . "<br>");
echo(round(-1.5,0,PHP_ROUND_HALF_EVEN) . "<br>");
echo(round(1.5,0,PHP_ROUND_HALF_ODD) . "<br>");
echo(round(-1.5,0,PHP_ROUND_HALF_ODD));
?>
❮ Référence mathématique PHP