W3docs

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 = ",") : string

Parameters

ParameterDescription
$numberThe number to format. This is the only required argument.
$decimalsHow many digits to show after the decimal point. Defaults to 0.
$decimal_separatorThe character used as the decimal point. Defaults to ".".
$thousands_separatorThe 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:

php— editable, runs on the server

Output:

1,235

The .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.57

The 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.89

To remove thousands grouping entirely, pass an empty string as the fourth argument:

<?php
echo number_format(1234567.891, 2, ".", "");
?>

Output:

1234567.89

How 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
-10

Common gotchas

  • The result is a string, not a number. Adding the thousands separator means "1,235" + 1 will 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, use floatval() 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. Add 0 to the value (or use abs() on near-zero values) if you need to avoid that.
  • For locale-aware currency (symbols, locale rules, accounting formats), consider PHP's NumberFormatter from the intl extension instead of building the string by hand.
  • round() — round a number to a given precision and get back a float.
  • sprintf() and printf() — 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.

Practice

Practice
What is the purpose of the number_format() function in PHP?
What is the purpose of the number_format() function in PHP?
Was this page helpful?