number_format()

number_format() — Format a number with grouped thousands.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Formats a number with grouped thousands and optionally decimal digits using the rounding half up rule.

Signature

number_format(float $num, int $decimals = 0, ?string $decimal_separator = ".", ?string $thousands_separator = ","): string

Parameters

ParameterTypeWhat it takes
$numfloatThe number being formatted.
$decimalsintSets the number of decimal digits. If 0, the decimal_separator is omitted from the return value. As of PHP 8.3.0, when the value is negative, num is rounded to decimals significant digits before the decimal point. Prior to PHP 8.3.0, negative values were ignored and handled the same as 0.
$decimal_separator?stringSets the separator for the decimal point.
$thousands_separator?stringSets the thousands separator.

Return value

A formatted version of num.

Example

A runnable snippet:

$number = 1234.56; // english notation (default) echo number_format($number), PHP_EOL; // 1,235 // French notation echo number_format($number, 2, ',', ' '), PHP_EOL; // 1 234,56 $number = 1234.5678; // english notation without thousands separator echo number_format($number, 2, '.', ''), PHP_EOL; // 1234.57

Changelog

VersionChange
8.3.0Added handling of negative values for decimals.
8.0.0Prior to this version, number_format() accepted one, two, or four parameters (but not three).
7.2.0number_format() was changed to not being able to return -0, previously -0 could be returned for cases like where num would be -0.01.

See also

sprintf(), printf(), sscanf()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page number_format() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.