PHP number_format() function

Example

Format the number:

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

Run Instances

Definition and Usage

The number_format() function formats numbers by grouping them in thousands.

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

Syntax

number_format(number,decimals,decimalpoint,separator)
Parameters Description
number

Required. The number to be formatted.

If no other parameters are set, the number will be formatted without a decimal point and with a comma (,) as the thousand separator.

decimals Optional. Specifies how many decimals. If this parameter is set, then the point (.) is used as the decimal point to format the number.
decimalpoint Optional. Specifies the string used as the decimal point.
separator

Optional. Specifies the string used as the thousand separator. Only the first character of the parameter is used. For example, "xxx" only outputs "x".

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

Technical Details

Return Value: Returns the formatted number.
PHP Version: 4+
Update Log:

Starting from PHP 5.4, this function is available in the parameters decimalpoint and separator Supports multibyte.

In older versions, only the first byte of each separator was used.

More Examples

Example 1

You want to return a price: one parameter rounds the number (formats without decimal places), two parameters give you the result you want:

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

Run Instances