number_format
Formats a number for display: decimals and a thousands separator.
Syntax
number_format(float $num, int $decimals = 0, ?string $decimal_separator = ".", ?string $thousands_separator = ","): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $num | float | The number. |
| $decimals | int | Optional. The number of decimal places. |
| $decimal_separator | ?string | Optional. The decimal point character. |
| $thousands_separator | ?string | Optional. The thousands separator. |
Return value
stringThe formatted number as a string.
Examples
The default form, and with cents:
<?php
echo number_format(1234567.891), "\n";
echo number_format(1234567.891, 2);Output:
1,234,568
1,234,567.89The European form: a space and a comma:
<?php
echo number_format(1234567.891, 2, ',', ' ');Output:
1 234 567,89Notes
The result is a string, and you cannot compute with it further. Format the number at the very end, when printing.
See also
Related recipes
In the tutorial
- Numbers and maths — PHP's maths functions — rounding, number formatting, random numbers and the precision of fractions.