What is PHP number_format() Function?
If you want to improve readability of a number by adding grouped thousand separators or decimal points, then use PHP number_format() function.
Syntax:
number_format(number, decimals, decimal_separator, number_separator)
Parameters:
The Function has 1 required parameter and 3 optional parameters-
number (Required): It specifies a number. Check example 1.
decimals (Optional): It specifies the number of digits after the decimal point. The number is rounded to this precision. Check example 2.
decimal_separator (Optional): It specifies the character(s) to use as the decimal point. Default is dot (.). Check example 3.
number_separator (Optional): It specifies the character(s) to use as the thousands grouping separator. Default is comma (,). Check example 4.
Return Values:
The function returns the formatted number.
Examples:
Example 1:
<?php
$number = 1234.51;
echo number_format($number);
?>
Output:
1,235
Example 2:
<?php
$number = 1234.565;
echo number_format($number, 2);
?>
Output:
1,234.57
Example 3: You can use the custom thousand separator and decimal point separator.
<?php
$number = 1234.565;
echo number_format($number, 2, "'");
?>
Output:
1,234'57
Example 4: You can display 0 after decimal point.
<?php
$number = 0;
echo number_format($number, 2);
?>
Output:
0.00
Example 5: This function also works on negative number as expected.
<?php
$number = -1234.565;
echo number_format($number, 2);
?>
Output:
-1,234.57
Example 6: Sometimes you may not need thousands separators but only the decimal separator.
<?php
$number = 1500;
echo number_format($number, 2, ".", "");
?>
Output:
1500.00
Practical Usages of number_format() Function:
- In web applications, this function is used for displaying –
- prices,
- statistics,
- financial data,
- large numbers, or
- any human-readable numeric string.
Notes on number_format() Function:
- The function always returns a string not a number.
- When rounding a number, the function follows “half up” rule. In “half up” rule, the fractional part of exactly 0.5 is always rounded up to the next higher digit. So, 1.5 becomes 2, 99.5 becomes 100 etc. Check example 1.
- The function can handles negative numbers correctly.
- If you pass a string that isn’t a valid number, PHP will throw a error.
Caution:
The formatted number may not work for calculation. The formatted string is for display. Check example below-
<?php
$price = number_format(2.10, 2);
$total = $price + 100; // This may not work as expected.
echo "The price is: " . $total;
echo "<br />";
$price = 2.10;
$total = $price + 100;
$total = number_format($total, 2);
echo "The price is: " . $total;
?>
Output:
The price is: 102.1
The price is: 102.10
So, you should use it at the very end of your logic, right before displaying the value to the user. For formatting, always use raw number, not the formatted numbers.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP number_format() Function
The function is one of most frequently used functions in PHP that make number more readable. It is very fast and efficient even for large numbers.