PHP number_format() Funktion
Beispiel
Zahlen formatieren:
<?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>
Definition und Verwendung
Die Funktion number_format() formatiert eine Zahl mit gruppierten Tausendern.
Hinweis: Diese Funktion unterstützt einen, zwei oder vier Parameter (nicht drei).
Syntax
number_format(number,decimals,decimalpoint,separator)
Parameterwerte
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
Rückgabewert: | Gibt die formatierte Zahl zurück |
---|---|
PHP-Version: | 4+ |
Änderungsprotokoll: | Ab PHP 5.4 unterstützt diese Funktion mehrere Bytes in den Parametern decimalpoint und separator . In älteren Versionen wurde nur das erste Byte jedes Trennzeichens verwendet. |
Mehr Beispiele
Beispiel
Sie möchten einen Preis zurückgeben: Ein Parameter rundet die Zahl (sie wird ohne Dezimalstellen formatiert). Zwei Parameter sollten das gewünschte Ergebnis liefern:
<?php
$num = 1999.9;
$formattedNum = number_format($num)."<br>";
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
?>
❮ PHP-String-Referenz