Menu

PHP number_format(): Thousands Separator and Decimals

number_format(1234567.891, 2) returns "1,234,567.89": it rounds to the given decimals and adds a thousands separator. Learn the four parameters, European and Japanese formats, how it rounds, and why the result is a string.

This page includes runnable editors - edit, run, and see output instantly.

number_format($number, $decimals) returns the number as a string with a thousands separator and a fixed number of decimals: number_format(1234567.891, 2) returns "1,234,567.89". With one argument it rounds to a whole number: number_format(1234567.891) is "1,234,568".

The four parameters

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

The third argument is the character between the whole part and the decimals, the fourth the character between groups of three digits. Either can be any string, including an empty one or a multibyte character. Edit the separators below to see each style:

Locale data (CLDR) uses a narrow no-break space (U+202F) for French and a no-break space (U+00A0) for Russian, so a number never wraps across two lines. In HTML, pass "\u{202F}" or "\u{00A0}" as the separator instead of a plain space.

Format a number without a thousands separator

Pass '' as the fourth argument. You get a plain number with a fixed number of decimals, the shape payment gateways, CSV exports and <input type="number"> values expect:

For the last line, round() before the cast matters: 19.9 * 100 is stored as 1989.9999999999998, and (int) alone would cut it to 1989.

How number_format rounds

number_format() rounds half away from zero, the way round() does, so 0.5 becomes 1 and -2.5 becomes -3. It also corrects the tiny binary error in values like 2.675, which is why it can disagree with sprintf('%.2f'):

2.675 cannot be stored exactly in binary; the stored value is a hair below it. sprintf() formats that stored value and gets 2.67, while number_format() and round() treat it as the 2.675 you wrote. If you need a specific rounding rule, call round(), floor() or ceil() yourself first (see rounding and math) and pass the result to number_format().

Format money and other currencies

number_format() knows nothing about currencies, so you add the symbol yourself. Put negative signs before the symbol, and remember that yen has no decimals:

For locale-aware output with the right symbol position for every country, the intl extension's NumberFormatter does the whole job:

$fmt = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
echo $fmt->formatCurrency(1234.5, 'EUR'); // 1.234,50 € (a no-break space before €)

It requires the intl extension, which is not installed everywhere, so number_format() with your own symbol is the portable choice.

The result is a string, not a number

This is the most common bug with number_format(): formatting a value and then calculating with it. "1,234.50" is not a numeric string, so PHP reads only the 1 before the comma:

<?php
echo number_format(1234.5, 2) + 1;
Warning: A non-numeric value encountered in /home/index.php on line 2
2

Passing such a string back into number_format() is worse: it throws TypeError: number_format(): Argument #1 ($num) must be of type int|float, string given. Keep the raw number for math and format only at the moment you print. If you receive a formatted string from a form or a file, strip the separators first:

For padding, alignment or a plus sign in front of the number, use sprintf() instead; it does not add thousands separators, but it controls width and sign.

Frequently Asked Questions

How do I format a number with commas in PHP?

Call number_format($n): number_format(1234567) returns "1,234,567". Pass a second argument for decimals: number_format(1234567.891, 2) returns "1,234,567.89".

How do I use number_format without a comma?

Pass an empty string as the thousands separator: number_format(1234.5, 2, '.', '') returns "1234.50". This is the form payment APIs and CSV files usually expect.

Does number_format round or truncate?

It rounds, half away from zero: number_format(2.5) is "3", number_format(-2.5) is "-3" and number_format(2.675, 2) is "2.68". To cut off decimals instead, use floor() first: number_format(floor(2.679 * 100) / 100, 2) gives "2.67".

How do I format numbers the European way (1.234,56)?

Swap the separators with the third and fourth arguments: number_format(1234.56, 2, ',', '.') returns "1.234,56". French and Swiss styles use a space or an apostrophe as the fourth argument.

Why does number_format give a wrong result when I add to it?

number_format() returns a string such as "1,234.50", and PHP reads only the 1 before the comma when you do math with it. Keep the number for calculations and call number_format() only when you print it.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED