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:
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.
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:
1 2 3 4 5 6 7 |
<?php $number = 29999.8; $formattedNumber = number_format($number)."<br>"; echo $formattedNumber; $formattedNumber = number_format($number, 2); echo $formattedNumber; ?> |
29,999.80
Example:2
1 2 3 4 5 |
<?php echo number_format("10000")."<br>"; echo number_format("10000",2)."<br>"; echo number_format("10000",2,",","."); ?> |
10,000.00
10.000,00
Example:3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php $number1 = "777777.38"; // With out decimal point parameter echo number_format($number1)."\n"; // With decimal Point parameter echo number_format($number1, 3)."\n"; $number2 = "7777777.98"; // With out decimal point parameter // return Round value echo number_format($number2)."\n"; // With decimal Point parameter echo number_format($number2, 3)."\n"; // With All four parameters echo number_format("10000.88", 3, "#", "GGG"); ?> |
7,777,778
7,777,777.980
10GGG000#880
Example:4
Furthermore, suppose that if pass anything instead of numbers it gives warning.
1 2 3 4 5 6 7 |
<?php $number = "GFG"; // With out decimal point parameter echo number_format($number)."\n\n"; // With decimal Point parameter echo number_format($number, 3); ?> |
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.
1 2 3 4 5 6 |
<?php $number = 4000000; // passing 3 parameters It gives errors because function // accepting only 1, 2 or 4 parameters echo number_format($number, 3, ", "); ?> |
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.