Funkce PHP number_format() .
Příklad
Formát čísel:
<?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>
Definice a použití
Funkce number_format() formátuje číslo se seskupenými tisíci.
Poznámka: Tato funkce podporuje jeden, dva nebo čtyři parametry (ne tři).
Syntax
number_format(number,decimals,decimalpoint,separator)
Hodnoty parametrů
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 |
Technické údaje
Návratová hodnota: | Vrátí formátované číslo |
---|---|
Verze PHP: | 4+ |
Seznam změn: | Od PHP 5.4 tato funkce podporuje více bajtů v parametrech desetinná tečka a oddělovač . Ve starších verzích byl použit pouze první bajt každého oddělovače. |
Další příklady
Příklad
Chcete vrátit cenu: Jeden parametr zaokrouhlí číslo (bude formátováno bez desetinných míst). Dva parametry by měly poskytnout požadovaný výsledek:
<?php
$num = 1999.9;
$formattedNum = number_format($num)."<br>";
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
?>
❮ Odkaz na řetězec PHP