PHP number_format() Functie
❮ Referentie voor PHP-tekenreeksen
Voorbeeld
Formaat nummers:
<?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>
Definitie en gebruik
De functie number_format() maakt een getal op met gegroepeerde duizenden.
Opmerking: deze functie ondersteunt één, twee of vier parameters (niet drie).
Syntaxis
number_format(number,decimals,decimalpoint,separator)
Parameterwaarden
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 |
Technische details
Winstwaarde: | Retourneert het opgemaakte getal |
---|---|
PHP-versie: | 4+ |
Wijzigingslogboek: | Vanaf PHP 5.4 ondersteunt deze functie meerdere bytes in de parameters decimale punt en scheidingsteken . In oudere versies werd alleen de eerste byte van elk scheidingsteken gebruikt. |
Meer voorbeelden
Voorbeeld
U wilt een prijs retourneren: Eén parameter rond het getal af (het wordt opgemaakt zonder decimalen). Twee parameters zouden het gewenste resultaat moeten geven:
<?php
$num = 1999.9;
$formattedNum = number_format($num)."<br>";
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
?>
❮ Referentie voor PHP-tekenreeksen