CKTIS / PHP
number_format()
Formate un nombre en texte avec groupement et arrondi à la moitié supérieure en valeur absolue.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
number_format(float $num, int $decimals = 0, ?string $decimal_separator = ".", ?string $thousands_separator = ","): stringFormate un nombre en texte avec groupement et arrondi à la moitié supérieure en valeur absolue. Précisez les séparateurs, un séparateur de groupes vide les supprime. Depuis PHP 8.3 des décimales négatives arrondissent avant la virgule. Le résultat sert à l’affichage.
Signature
number_format(float $num, int $decimals = 0, ?string $decimal_separator = ".", ?string $thousands_separator = ","): string
Sortie de l’exemple
["1 234,50","1300","1.3"]
Exemples
<?php
echo json_encode([number_format(1234.5,2,","," "),number_format(1299,-2,".",""),number_format(1.25,1)]);
Résultat
["1 234,50","1300","1.3"]