Arrondir les nombres à virgule flottante en PHP

Sheeraz Gul 18 aout 2022
Arrondir les nombres à virgule flottante en PHP

En PHP, la fonction round() arrondit les nombres à virgule flottante. Ce tutoriel montre comment utiliser la fonction round() de PHP.

Arrondir les nombres à virgule flottante en PHP

La fonction round() arrondit un nombre à virgule flottante en PHP. Nous pouvons l’utiliser pour définir une valeur de précision spécifique qui arrondira les nombres, en gardant la valeur de précision en vue.

Cette valeur de précision peut être nulle ou négative. La fonction a trois paramètres ; la syntaxe de cette fonction est :

float round(number, precision, mode);

Où:

  1. Le number est le nombre à virgule flottante que nous voulons arrondir.

  2. Le paramètre precision est un paramètre optionnel qui décide du nombre de chiffres décimaux à arrondir. La valeur par défaut de precision est zéro.

  3. Le mode est un paramètre optionnel pour spécifier une constante, spécifiant le mode d’arrondi. Il y a quatre types de constantes pour le mode :

    3.1. PHP_ROUND_HALF_UP : ce mode indiquera à la méthode d’arrondir le nombre à partir de zéro.
    3.2. PHP_ROUND_HALF_DOWN : ce mode indiquera à la méthode d’arrondir le nombre vers zéro.
    3.3. PHP_ROUND_HALF_EVEN: Ce mode indiquera à la méthode d’arrondir le nombre vers la valeur paire la plus proche.
    3.4. PHP_ROUND_HALF_ODD : ce mode indiquera à la méthode d’arrondir le nombre vers la valeur impaire la plus proche.

La valeur de retour pour cette méthode est le nombre arrondi. Essayons un exemple de cette méthode.

<?php
$RoundValue1=round(10.3);
$RoundValue2=round(12.5);
$RoundValue3=round(13.6);
$RoundValue4=round(14.6,0);
$RoundValue5=round(6.97553,2);
$RoundValue6=round(7.76521,-3);
$RoundValue7=round(8.063,2);
$RoundValue8=round(9.076,2);
$RoundValue9=round(6.97553, PHP_ROUND_HALF_UP);
$RoundValue10=round(6.97553, PHP_ROUND_HALF_DOWN);
$RoundValue11=round(6.97553, PHP_ROUND_HALF_EVEN);
$RoundValue12=round(6.97553, PHP_ROUND_HALF_ODD) ;


echo "The Round Value for (10.3)  is : ". $RoundValue1 . "<br /><br />" ;
echo "The Round Value for (12.5)  is : ". $RoundValue2 . "<br /><br />" ;
echo "The Round Value for (13,6)  is : ". $RoundValue3 . "<br /><br />" ;
echo "The Round Value for (14.6,0)  is : ". $RoundValue4 . "<br /><br />" ;
echo "The Round Value for (6.97553,2)  is : ". $RoundValue5 . "<br /><br />" ;
echo "The Round Value for (7.76521,-3) is : ". $RoundValue6 . "<br /><br />" ;
echo "The Round Value for (8.063,2)  is : ". $RoundValue7 . "<br /><br />" ;
echo "The Round Value for (9.076,2)  is : ". $RoundValue8 . "<br /><br />" ;
echo "The Round Value for (6.97553,2, PHP_ROUND_HALF_UP)  is : ". $RoundValue9 . "<br /><br />" ;
echo "The Round Value for (6.97553,2, PHP_ROUND_HALF_DOWN) is : ". $RoundValue10 . "<br /><br />" ;
echo "The Round Value for (6.97553,2, PHP_ROUND_HALF_EVEN)  is : ". $RoundValue11 . "<br /><br />" ;
echo "The Round Value for (6.97553,2, PHP_ROUND_HALF_ODD)  is : ". $RoundValue12 . "<br /><br />" ;
?>

Le code ci-dessus montre 12 versions différentes de l’utilisation de la méthode round(). Voir la sortie :

The Round Value for (10.3) is : 10

The Round Value for (12.5) is : 13

The Round Value for (13,6) is : 14

The Round Value for (14.6,0) is : 15

The Round Value for (6.97553,2) is : 6.98

The Round Value for (7.76521,-3) is : 0

The Round Value for (8.063,2) is : 8.06

The Round Value for (9.076,2) is : 9.08

The Round Value for (6.97553,2, PHP_ROUND_HALF_UP) is : 7

The Round Value for (6.97553,2, PHP_ROUND_HALF_DOWN) is : 6.98

The Round Value for (6.97553,2, PHP_ROUND_HALF_EVEN) is : 6.976

The Round Value for (6.97553,2, PHP_ROUND_HALF_ODD) is : 6.9755
Auteur: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Article connexe - PHP Number