Example
Formatting numbers:
<?php echo number_format("1000000")."<br>"; echo number_format("1000000",2)."<br>"; echo number_format("1000000",2,",","."); ?>
Definition and usage
number_format() function formats numbers by thousands grouping.
Note: This function supports one, two or four parameters (not three).
Syntax
number_format(number,decimals,decimalpoint,separator)
Parameters Description
number The number to format. If no other parameters are set, the number is formatted without a decimal point and with a comma (,) as the thousands separator.
decimals Optional. Specify the number of decimal places. If this parameter is set, numbers are formatted using a period (.) as the decimal point.
decimalpoint Optional. Specifies the string used as the decimal point.
separator Optional. Specifies the string used as the thousands separator. Only the first character of the parameter is used. For example, "xxx" only outputs "x".
Note: If this parameter is set, all other parameters are required.
Technical details
Return value: Return a formatted number.
PHP Version: 4+
##Change Log: Since PHP 5.4, this The function supports multibytes in the decimalpoint and separator parameters. In previous versions, the value used the first byte of each delimiter. More ExamplesExample 1You want to return a price: one argument will round the number (formatted without decimal place form), two parameters will give you the result you want:<?php $num = 1999.9; $formattedNum = number_format($num)."<br>"; echo $formattedNum; $formattedNum = number_format($num, 2); echo $formattedNum; ?>
number_format(number,decimals,decimalpoint,separator)
The first and The two parameters are required, the third and fourth are optional. However, in actual testing, the third and fourth parameters must exist at the same time, that is, either both are set or neither is set.
The third and fourth parameters are not set:
Number_format(13526, 2); echo 13,526.00;
The third and fourth parameters are set
Number_format(23125, 2, ‘.',''); echo 23125.00;
这时再对这处理后的数字进行运算的话则会正确执行!
该函数的第三个参数表示 ‘小数点'位置用什么来表示,可以默认 . ,也可以设置成‘,'等其他符号。Ps:但我相信没人会这么干。
第四个则表示每隔 千位时用什么来分割数字。如果没什么特殊要求,又要进行运算的话最好设置为空。
The above is the detailed content of PHP function number_format() that formats numbers by grouping by thousands. For more information, please follow other related articles on the PHP Chinese website!