Home >>PHP String Functions >PHP number_format() Function
PHP number_format() function is used to format a number with grouped thousands. This function returns the formatted number on success and in the case of failure it gives E_WARNING. This function accepts one, two, or four parameters but not three.
Syntax:-
number_format($number,$decimals,$decimalpoint,$separator);
Parameter | Description |
---|---|
number | This is a required parameter. This parameter contains the number to be formatted. If no other parameters are set, the number will be formatted without decimals and with comma (,) as default. |
decimals | This is an optional parameter. This parameter defines how many decimals. The given input number will be formatted with a dot (.) as decimal point. |
decimalpoint | This is an optional parameter. This parameter defines what string to use for decimal point. |
separator | This is an optional parameter. This parameter defines what string to use for thousands separator. |
Here is an example of number_format() function in PHP:
<html> <body> <?php $num = 100000; echo number_format($num)."<br>"; echo number_format($num,2)."<br>"; echo number_format($num,2,",","."); ?> </body> </html>
Here is another example of number_format() function in PHP:
<html> <body> <?php $num = 9999.9; echo number_format($num)."<br>"; echo number_format($num,2)."<br>"; ?> </body> </html>