Example 1
Format numbers:
<?php echo number_format("1000000").""; echo number_format("1000000",2).""; echo number_format("1000000",2,",","."); ?>
Definition And usage
number_format()
function formats a number by grouping by thousands.
Note: This function supports one, two or four parameters (not three).
Syntax
##number_format(number,decimals,decimalpoint,separator)
Description | |
---|---|
Required. 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. | |
Optional. Specify the number of decimals. If this parameter is set, numbers are formatted using a period (.) as the decimal point. | |
Optional. Specifies the string used as the decimal point. | |
Optional. Specifies the string used as 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. |
Example 2
You want to return a price: one argument will round the number (formatted without decimal place form), the two parameters will give you the result you want:<?php $num = 1999.9; $formattedNum = number_format($num).""; echo $formattedNum; $formattedNum = number_format($num, 2); echo $formattedNum; ?>
Related learning recommendations:
The above is the detailed content of Exploring the PHP number_format function principle and example analysis. For more information, please follow other related articles on the PHP Chinese website!