PHP 8 reference · String Functions

PHP number_format() Function

Format a number with grouped thousands and a fixed number of decimals, such as 1,234,567.89 or 1.234,57.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
num The number to format.
decimals optional Digits after the decimal point; negative rounds to tens, hundreds… Default: 0
decimal_separator optional The decimal point character. Default: "."
thousands_separator optional The thousands separator. Default: ","

Return value

The formatted number as a string.

Examples

Thousands and decimalsTry it yourself
<?php
echo number_format(1234567.891), "\n";
echo number_format(1234567.891, 2), "\n";
echo number_format(1234567.891, 2, ",", "."), "\n";

Output

1,234,568
1,234,567.89
1.234.567,89
RoundingTry it yourself
<?php
echo number_format(0.5), "\n";      // rounds half away from zero
echo number_format(1234567, -3), "\n"; // PHP 8.3+

Output

1
0

Common mistakes

  • The result is a string with separators, so don't do more maths with it. Format only for display.
  • For locale-aware formatting (currencies, Indian grouping) use NumberFormatter from the intl extension.

Version history

VersionChange
PHP 4.0Added
PHP 8.0Can be called with three arguments; before, it accepted one, two or four.
PHP 8.3Negative decimals round before the decimal point.

In other languages

Was this page helpful?