PHP number_format () function
Examples
Digital format:
<?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>
Running instance »
Definition and Usage
number_format () function to format with thousands grouping by numbers.
Note: This function supports one, two or four parameters (not three).
grammar
number_format( number,decimals,decimalpoint,separator )
参数 | 描述 |
---|---|
number | 必需。要格式化的数字。如果未设置其他参数,则数字会被格式化为不带小数点且以逗号(,)作为千位分隔符。 |
decimals | 可选。规定多少个小数。如果设置了该参数,则使用点号(.)作为小数点来格式化数字。 |
decimalpoint | 可选。规定用作小数点的字符串。 |
separator | 可选。规定用作千位分隔符的字符串。仅使用该参数的第一个字符。比如 "xxx" 仅输出 "x"。 注释:如果设置了该参数,那么所有其他参数都是必需的。 |
technical details
return value: | Returns formatted numbers. |
---|---|
PHP version: | 4+ |
Update log: | Since PHP 5.4 onwards, the function supports multi-byte parameters decimalpoint and the separator. In previous releases, the value of the use of each of the first byte of the separator. |
More examples
Example 1
You want to return a price: a numeric parameter will be rounded (formatted as a form with no decimal places), two parameters will give the results you want:
<?php
$num = 1999.9;
$formattedNum = number_format($num)."<br>";
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
?>
$num = 1999.9;
$formattedNum = number_format($num)."<br>";
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
?>
Running instance »
PHP String Reference