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
Syntax
number_format(float $num, int $decimals = 0, ?string $decimal_separator = ".", ?string $thousands_separator = ","): string
Parameters
| Parameter | Description |
|---|---|
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
<?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
<?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
NumberFormatterfrom the intl extension.
Version history
In other languages
number.toLocaleString()· JavaScriptnumber.toFixed()· JavaScript
Related
sprintf()· Return a formatted string built from a template with placeholders like %s, %d and %.2f. Format numbers, pad and align.- PHP Numbers and Math · tutorial