number_format – PHP String Functions

Syntax :

number_format ( number, decimals, decimal_point, thousands_sep);

Description :

number_format() function helps in formating a number (Floating number) with grouped thousands.

Note : This function takes either one, two, or four parameters (not three).

Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description

number, Required, Float , It is the input number.

decimals, Optional, Integer , It specifies the number for the decimal point.

decimal_point, Optional, String , It specifies the separator for decimal point.

thousands_sep, Optional, String , It specifies the separator for thousands.

[/table]


Output :

It returns the formatted number.


ChangeLog :

[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Version, Description
PHP 7.2.0, number_format() has been changed not to return -0. However in earlier version -0 could be returned for cases like where number would be -0.05.
PHP 5.4.0 , It supports multiple bytes in dec_point and thousands_sep now. Only the first byte of each separator was used in older versions..
[/table]


number_format() – PHP Functions Example 1 :
<?php
$number=999999;
echo number_format($number).'<br>';
echo number_format($number, 2).'<br>';
echo number_format($number, 3).'<br>';
echo number_format($number, 2, ',', '.');
?>

Output of above code in the browser is as below:

999,999
999,999.00
999,999.000
999.999,00

number_format() – PHP Functions Example 2 :
<?php
$number=999999.99;
echo number_format($number).'<br>'; //One parameter will round the number. 
echo number_format($number, 2).'<br>';
echo number_format($number, 3).'<br>';
echo number_format($number, 2, ',', '.').'<br>';
echo number_format($number, 2, ',', 'x').'<br>';
?>

Output of above code in the browser is as below:

1,000,000
999,999.99
999,999.990
999.999,99
999×999,99

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *