Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

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

Parameters

ParameterTypeDescription
$numfloatThe number.
$decimalsintOptional. The number of decimal places.
$decimal_separator?stringOptional. The decimal point character.
$thousands_separator?stringOptional. 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.89

The European form: a space and a comma:

<?php
 
echo number_format(1234567.891, 2, ',', ' ');

Output:

1 234 567,89

Notes

The result is a string, and you cannot compute with it further. Format the number at the very end, when printing.

See also

  • round — Rounds a number to a given number of digits.
  • sprintf — Returns a string built from a format template.

Related recipes

In the tutorial

  • Numbers and maths — PHP's maths functions — rounding, number formatting, random numbers and the precision of fractions.

Thanks for reading!

Donate to the site — we work hard for you.