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
| Parameter | Type | What it takes |
|---|---|---|
| $num | float | The number being formatted. |
| $decimals | int | Sets 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 | ?string | Sets the separator for the decimal point. |
| $thousands_separator | ?string | Sets 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
| Version | Change |
|---|---|
| 8.3.0 | Added handling of negative values for decimals. |
| 8.0.0 | Prior to this version, number_format() accepted one, two, or four parameters (but not three). |
| 7.2.0 | number_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
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.