PHP number_format() Funzione
❮ Riferimento alla stringa PHP
Esempio
Formatta numeri:
<?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>
Definizione e utilizzo
La funzione number_format() formatta un numero con migliaia raggruppate.
Nota: questa funzione supporta uno, due o quattro parametri (non tre).
Sintassi
number_format(number,decimals,decimalpoint,separator)
Valori dei parametri
Parameter | Description |
---|---|
number | Required. The number to be formatted. If no other parameters are set, the number will be formatted without decimals and with comma (,) as the thousands separator. |
decimals | Optional. Specifies how many decimals. If this parameter is set, the number will be formatted with a dot (.) as decimal point |
decimalpoint | Optional. Specifies what string to use for decimal point |
separator | Optional. Specifies what string to use for thousands separator. Only the first character of separator is used. For example, "xxx" will give the same output as "x" Note: If this parameter is given, all other parameters are required as well |
Dettagli tecnici
Valore di ritorno: | Restituisce il numero formattato |
---|---|
Versione PHP: | 4+ |
Registro delle modifiche: | A partire da PHP 5.4, questa funzione supporta più byte nei parametri decimalpoint e separator . Nelle versioni precedenti veniva utilizzato solo il primo byte di ciascun separatore. |
Altri esempi
Esempio
Vuoi restituire un prezzo: un parametro arrotonda il numero (sarà formattato senza decimali). Due parametri dovrebbero dare il risultato desiderato:
<?php
$num = 1999.9;
$formattedNum = number_format($num)."<br>";
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
?>
❮ Riferimento alla stringa PHP