PHP number_format | Understanding the PHP number format function Role

Summary: in this article, you will learn PHP number format function which is used to format a number with grouped thousands, let’s understand the PHP number_format() function with Example in details.

php number_format() Definition and Usage

The PHP number_format() function is an inbuilt function that is used to format a number (Floating number) with grouped thousands. furthermore, it returns the formatted number on success otherwise it provides E_WARNING on failure.

Syntax:

string number_format ( $number, $decimals, $decimalpoint, $sep )

Parameters:

There are the four parameters which accept these function as mentioned above and described below:

Parameter Description
$number: Required. it specified the number to be formatted. furthermore, if no other parameters are set, then the number will be formatted without decimals & with the comma (, ) as the thousands separator.
$decimals: Optional. it is used to specifies decimals. If this parameter is set, then the number will be formatted with a dot (.) as the decimal point.
$decimalpoint: Optional. it is used to specifies the string to use for the decimal point.
$sep: Optional. it is used to specifies the string to use for the thousands of separators. furthermore, if this parameter is given, then all other parameters are required.

Return Value: It will returns Formatted Number in case success, otherwise it will gives E_WARNING in failure.

.
Important Note:
This function will accept either one, two, or four parameters (not three):
If only one parameter is given, then the number will be formatted without decimals, but with a comma (",") between each group of thousands.
If two parameters are given, then the number will be formatted with decimals with a dot (".") in ahead or front, and a comma (",") between each group of thousands.
If all four(4) parameters are given, then the number will be formatted with the decimals, dec_point instead of a dot (".") before the decimals & thousands_sep instead of a comma (",") b/w every group of thousands.

Technical Details

Return Value: it’s Returns the formatted number in case success
PHP Version: 4+
Changelog: As of PHP 5.4, this function will support the multiple bytes in the parameters decimal-point and separator. furthermore, only the first byte of every separator was utilized in older versions.

PHP number_format() Function Example

Example:1

suppose that you want to return a price, furthermore, one parameter will round the number i.e. it will be formatted without decimals. here, two parameters should give the result you want:

30,000
29,999.80
Example:2

10,000
10,000.00
10.000,00
Example:3

777,777.380
7,777,778
7,777,777.980
10GGG000#880
Example:4

Furthermore, suppose that if pass anything instead of numbers it gives warning.

Warning: number_format() expects parameter 1 to be float, string given in C:\wamp64\www\index.php on line 4
Warning: number_format() expects parameter 1 to be float, string given in C:\wamp64\www\index.php on line 6
Example:5

This php number_format() function does not accept three parameters, its only accept 1, 2 or 4 parameters.

Warning: Wrong parameter count for number_format() in C:\wamp64\www\index.php on line 5

Conclusion:

In this article, you have learned php number_format() function, which is used to format a number (Floating number) with grouped thousands. I hope you will enjoy it!. if have any query then contact on info@tutorialscan.com. I will try to resolve the problem.