PHP number_format()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Format a number:

<?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>

Run example »

Definition and Usage

The number_format() function formats a number with grouped thousands.

Note:This function supports one, two, or four parameters (not three).


Syntax

number_format(number,decimals,decimalpoint,separator)

Parameter Description
number Required. The number to be formatted. If no other parameters are set, the number is formatted without decimals and with a comma (,) as the thousands separator.
decimals Optional. Specifies how many decimals. If this parameter is set, the number is formatted with a dot (.) as the decimal point.
decimalpoint Optional. Specifies the string to be used as the decimal point.
separator Optional. Specifies the string to be used as the thousands separator. Only the first character of this parameter is used. For example, "xxx" outputs only "x".

Note:If this parameter is set, all other parameters are required.

Technical Details

Return Value: Returns the formatted number.
PHP Version: 4+
Changelog: Since PHP 5.4, this function supports multibyte in the parameterdecimalpointandseparatorIn previous versions, the value used the first byte of each separator.


More Examples

Example 1

You want to return a price: one parameter will round the number (formatted without decimal places), two parameters will give you the desired result:

<?php
$num = 1999.9;
$formattedNum = number_format($num)."<br>";
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
?>

Run example »


PHP String 参考手册PHP String Reference Manual Other Extensions