PHP number_format() for Thousands Separators and Decimals

PHP number_format() returns a string with your chosen thousands separator and number of decimal places. number_format(123456.123456, 2) yields 123,456.12. Omit the precision to round to a whole number.

Set the display format with arguments

Pass the number first and use the remaining arguments to control the output. PHP 8 also accepts three arguments. When changing separators, specify both so the output is unambiguous. See the PHP manual on number_format().

Argument Default Purpose
num Required Number to format.
decimals 0 Number of decimal places. With 0, the decimal point is omitted.
decimal_separator '.' String to put before the fractional digits.
thousands_separator ',' String to insert between groups of three digits. Use '' to disable grouping.

Setting the decimal separator to a comma on its own can leave the default grouping comma in place. Set both explicitly, as in number_format($number, 2, ',', '.').

Compare decimal precision and separators

With PHP installed, save this example in UTF-8 as number-format-example.php. Run the first command in that folder to check its syntax and the second to execute it. See the PHP basics and execution guide if this is your first PHP script.

<?php
$number = 123456.123456;

echo number_format($number), PHP_EOL;
echo number_format($number, 1), PHP_EOL;
echo number_format($number, 2), PHP_EOL;
echo number_format($number, 4), PHP_EOL;
echo number_format($number, 2, ',', '.'), PHP_EOL;
echo number_format($number, 2, '.', ''), PHP_EOL;
php -l number-format-example.php
php number-format-example.php

The first four lines vary the decimal precision. The fifth uses a decimal comma and a grouping point. The last line omits grouping.

123,456
123,456.1
123,456.12
123,456.1235
123.456,12
123456.12

Check rounding and negative precision

Reducing the precision rounds the value. A halfway value rounds away from zero. At one decimal place, 1.25 becomes 1.3 and -1.25 becomes -1.3. Save and run the next example as a separate PHP file.

<?php
$number = 123456.123456;

echo number_format(1.25, 1), PHP_EOL;
echo number_format(-1.25, 1), PHP_EOL;
echo number_format($number, -1), PHP_EOL;
echo number_format($number, -2), PHP_EOL;
echo number_format($number, -3), PHP_EOL;
1.3
-1.3
123,460
123,500
123,000

Since PHP 8.3, -1, -2, and -3 round to tens, hundreds, and thousands respectively. PHP 8.2 and earlier treated negative precision as 0, so the last three lines differ. Check the version with php -v.

Handle numeric strings and missing input

Without strict typing, you can pass a numeric string such as '123456.123456'. Passing an empty string directly throws a TypeError in PHP 8. Coercing null to the required number is deprecated as of PHP 8.1, as described in the PHP 8.1 deprecation notes. This example uses strict typing, tests input with is_numeric(), and formats only finite numbers.

<?php
declare(strict_types=1);

function formatAmount(mixed $value): string
{
    if (!is_numeric($value)) {
        return '—';
    }

    $number = (float) $value;
    if (!is_finite($number)) {
        return '—';
    }

    return number_format($number, 2, '.', ',');
}

$inputs = ['123456.123456', '', null, '0', '1,234.56', '1e309'];

foreach ($inputs as $input) {
    echo formatAmount($input), PHP_EOL;
}
123,456.12
—
—
0.00
—
—

Run it as another PHP file. It formats valid input and shows — for missing or unsupported input. The string '0' still gives 0.00. A formatted input such as '1,234.56' does not pass the numeric-string check, and '1e309' is rejected because conversion produces a nonfinite number.

Keep formatted text separate from numeric values

The result is a string. Keep the original numbers for storage and calculations, and call number_format() when displaying them. PHP may no longer recognize a string with grouping separators as a number.

For locale-dependent grouping and separators, use NumberFormatter from PHP’s intl extension.