String Functions

PHP number_format() Function

Topic: PHP String ReferencePrev|Next


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

The following table summarizes the technical details of this function.

Return Value: Returns the formatted number.
Changelog: Since PHP 7.2.0 this function will not return -0, previously -0 could be returned for cases like where number would be -0.01.
Version: PHP 4+


The basic syntax of the number_format() function is given with:

number_format(number, decimals, decimal_point, separator);

The following example shows the number_format() function in action.

echo number_format("1234000");    // Outputs: 1,234,000
echo number_format("1234000", 2); // Outputs: 1,234,000.00


The number_format() function accepts the following parameters.

Parameter Description
number Required. Specifies the number to be formatted.
decimals Optional. Specifies the number of decimal points.
decimal_point Optional. Specifies what string to use for decimal point.
separator Optional. Specifies what string to use for thousands separator.

This function accepts either one, two, or four parameters (not three):

  • If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands.
  • If two parameters are given, number will be formatted with decimals with a dot (".") in front, and a comma (",") between every group of thousands.
  • If all four parameters are given, number will be formatted according to the values specified for the individual parameters (see the table above).

More Examples

Here're some more examples showing how number_format() function actually works:

If decimals parameter is ignored or not specified correctly this function may round off the number. So be careful while formatting the data like product prices. Here's an example:

echo number_format("1234.99");    // Outputs: 1,235
echo number_format("1234.99", 1); // Outputs: 1,235.0
echo number_format("1234.99", 2); // Outputs: 1,234.99
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties