PHP Function Reference

PHP number_format() Function



The PHP number_format() function formats a given number with grouped thousands and optionally decimal digits.

Note: Prior to PHP 8.0.0, this function accepted one, two, or four parameters (but not three).

Syntax

number_format(number, decimals, decimal_separator, thousands_separator)

Parameters

number Required. Specify the number to be formatted.
decimals Optional. Specify the number of decimal digits. If 0, the decimal_separator is omitted from the return value. Default is 0.
decimal_separator Optional. Specify the string to use for separator for the decimal point. Default is .
thousands_separator Optional. Specify the string to use for thousands separator. Default is ,

Return Value

Returns a formatted version of the given number.

Example:

The example below shows the usage of this function.

<?php
$num = 12345.6789;

//default version
echo number_format($num)."\n";

//3 decimal digits with default decimal separator
echo number_format($num, 3)."\n";

//3 decimal digits with . as decimal separator
echo number_format($num, 3, '.')."\n";

//3 decimal digits with . as decimal separator
//and , as thousand separator
echo number_format($num, 3, '.', ',')."\n";

//3 decimal digits with . as decimal separator
//and space as thousand separator
echo number_format($num, 3, '.', ' ')."\n";

//3 decimal digits with . as decimal separator
//and no thousand separator
echo number_format($num, 3, '.', '')."\n";
?>

The output of the above code will be:

12,346
12,345.679
12,345.679
12,345.679
12 345.679
12345.679

❮ PHP String Reference