number_format()
Our article is about the PHP function number_format(), which is used to format a number with grouped thousands. This function is useful for working with numbers
The PHP number_format() function formats a number with grouped thousands and an optional fixed number of decimal places. It returns a string, which makes it the go-to function for displaying prices, statistics, and any number a human will read. It is purely about presentation: it does not change the underlying value, only how it is rendered.
This chapter covers the syntax, every parameter, how rounding works, and the common gotchas to watch out for.
Syntax
number_format(float $number, int $decimals = 0, string $decimal_separator = ".", string $thousands_separator = ",") : stringParameters
| Parameter | Description |
|---|---|
$number | The number to format. This is the only required argument. |
$decimals | How many digits to show after the decimal point. Defaults to 0. |
$decimal_separator | The character used as the decimal point. Defaults to ".". |
$thousands_separator | The character used to separate groups of thousands. Defaults to ",". |
The function always returns a string, so the output is ready to print or concatenate but should not be used for further arithmetic.
Basic example
With only the number passed in, number_format() rounds to a whole number and adds commas between thousands:
Output:
1,235The .56 fractional part caused the value to round up to 1235, and a comma was inserted between the thousands.
Controlling the decimal places
Pass a second argument to keep a fixed number of decimals. This is what you almost always want for money:
<?php
echo number_format(1234.567, 2); // two decimal places
?>Output:
1,234.57The third decimal (7) is rounded into the second place. Notice that the number of decimals is fixed even when the original value has fewer — number_format(5, 2) returns "5.00".
Custom separators (international formats)
The third and fourth arguments let you switch separators. Many European locales use a period for thousands and a comma for the decimal point:
<?php
$amount = 1234567.891;
echo number_format($amount, 2, ".", ",") . "\n"; // US / UK style
echo number_format($amount, 2, ",", ".") . "\n"; // German / Spanish style
echo number_format($amount, 2, ".", " "); // space-grouped
?>Output:
1,234,567.89
1.234.567,89
1 234 567.89To remove thousands grouping entirely, pass an empty string as the fourth argument:
<?php
echo number_format(1234567.891, 2, ".", "");
?>Output:
1234567.89How rounding works
number_format() rounds the value to the requested precision using round half away from zero — the same default as the round() function. A trailing .5 rounds up in magnitude:
<?php
echo number_format(2.5) . "\n"; // 3
echo number_format(0.5) . "\n"; // 1
echo number_format(-9.5); // -10
?>Output:
3
1
-10Common gotchas
- The result is a string, not a number. Adding the thousands separator means
"1,235" + 1will not behave as arithmetic. If you need to keep computing with the value, format it only at the moment you display it. To turn user input that contains separators back into a number, usefloatval()after stripping the grouping characters. - It rounds, it does not truncate.
number_format(9.999, 2)returns"10.00", not"9.99". For currency math, round your values deliberately before formatting. - Negative zero can appear. Very small negative numbers may render as
-0.00. Add0to the value (or useabs()on near-zero values) if you need to avoid that. - For locale-aware currency (symbols, locale rules, accounting formats), consider PHP's
NumberFormatterfrom theintlextension instead of building the string by hand.
Related functions
round()— round a number to a given precision and get back a float.sprintf()andprintf()— format numbers (and other values) with%placeholders such as%.2f.floatval()— convert a string back into a floating-point number.- PHP Math functions — the broader set of numeric helpers.