PHP number_format() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, number_format() formats a number with grouped thousands and a chosen number of decimals.

It returns a string, so it is meant for display and not for further math.

Syntax

Call number_format() with the values you need. Start with this line.

PHP
$text = number_format(1234567.891, 2);
CallWhat it does
number_format(1234567.891)Returns "1,234,568".
number_format(1234567.891, 2)Returns "1,234,567.89".
number_format(1234567.891, 2, ",", ".")Returns "1.234.567,89". European style.

The number is rounded, not cut off. Half values round away from zero.

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

Parameters

ParameterDescription
numRequired. The number to format.
decimalsOptional. How many digits to show after the decimal point. Default is 0.
decimal_separatorOptional. The character for the decimal point. Default is ".".
thousands_separatorOptional. The character between thousands. Default is ",". Use an empty string for none.

Return Value

Return ValueDescription
stringThe formatted number.

PHP Examples

Example 1 — Format Thousands and Decimals

Format the same number three different ways.

PHP
<?php
$n = 1234567.891;

echo number_format($n), PHP_EOL;
echo number_format($n, 2), PHP_EOL;
echo number_format($n, 2, ",", "."), PHP_EOL;

How It Works

  1. No decimals given. The default is 0, so the number rounds up.
  2. Ask for 2 decimals. You see .89 after rounding.
  3. Swap separators. Comma for decimals and dot for thousands.
  4. Pick the style. Use the one your users expect.

Example 2 — Rounding and Negative Zero

See how half values round and how a tiny negative number prints.

PHP
<?php
echo number_format(0.5), PHP_EOL;
echo number_format(2.5), PHP_EOL;
echo number_format(-0.01), PHP_EOL;

How It Works

  1. 0. 5 rounds up. Halves round away from zero.
  2. 2. 5 also rounds up. It becomes 3, not 2.
  3. Tiny negatives. -0.01 rounds to zero and prints 0 in PHP 8.
  4. Older versions. PHP 7 could print -0 here.

Example 3 — Price with No Thousands Separator

Pass an empty string as the last argument for a clean value like 1999.50.

PHP
<?php
$price = 1999.5;

echo number_format($price, 2, ".", ""), PHP_EOL;
echo number_format($price, 2), PHP_EOL;

How It Works

  1. Set a price. It is 1999.5.
  2. Use two decimals. The missing zero is added.
  3. Drop the separator. An empty string removes the comma.
  4. Choose by use. Raw style suits forms and APIs, grouped style suits display.

Frequently Asked Questions

number_format() formats a number with grouped thousands, a chosen number of decimal places, and custom separators, and returns the result as a string.
string number_format(float $num, int $decimals = 0, ?string $decimal_separator = ".", ?string $thousands_separator = ","). Only the number is required.
Because formatted numbers contain separators. Do your math first, then format only for display.
Pass an empty string as the fourth argument, like number_format($n, 2, ".", "").
It rounds. For example number_format(1.999, 2) gives "2.00". Use floor() first if you need to truncate.

Next: ord()

ord() returns the ASCII value of the first character of a string.

ord() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful