Working with numbers in PHP often requires formatting for better readability. Whether you are displaying prices in an eCommerce store, showing statistics, or handling financial calculations, PHP’s built-in number_format()
function is a powerful tool.
This article will walk you through the syntax, parameters, examples, and different use cases of number_format()
in PHP. By the end, you’ll be able to format numbers like a pro, making your web applications more user-friendly.
🔹 What is PHP number_format()?
The number_format()
function in PHP is used to format numbers with grouped thousands and specific decimal places. It is especially useful for:
- Formatting currency values (e.g.,
1,234.56
) - Adding commas or dots as thousand separators
- Defining the number of decimal points
- Supporting international number formatting styles
In simple words, number_format()
makes large numbers easier to read and display.
🔹 PHP number_format() Syntax
The syntax of the number_format()
function looks like this:
number_format(float $number, int $decimals = 0, ?string $decimal_separator = ".", ?string $thousands_separator = ","): string
Parameters:
- $number → The number you want to format.
- $decimals (optional) → The number of decimal points (default is
0
). - $decimal_separator (optional) → Character for separating decimals (default is
"."
). - $thousands_separator (optional) → Character for separating thousands (default is
","
).
🔹 Example 1: Basic Usage of number_format()
If you just want to format a large number with commas, you can use the default settings.
<?php
echo number_format(1234567);
// Output: 1,234,567
?>
✅ Explanation:
The function added commas as thousand separators and removed any decimals since we didn’t specify them.
🔹 Example 2: Formatting with Decimal Places
You can also specify how many decimal places should be displayed.
<?php
echo number_format(1234567.891, 2);
// Output: 1,234,567.89
?>
✅ Explanation:
Here, the number is rounded to 2 decimal places and formatted with commas. This is ideal for currency values.
🔹 Example 3: Using Custom Decimal and Thousand Separators
Different countries use different number formats. For example:
- US format:
1,234,567.89
- European format:
1.234.567,89
Let’s see how to handle this:
<?php
echo number_format(1234567.891, 2, ",", ".");
// Output: 1.234.567,89
?>
✅ Explanation:
We replaced the decimal separator with a comma ","
and the thousand separator with a dot "."
.
🔹 Example 4: Formatting Currency Values
The number_format()
function is widely used in eCommerce websites to display prices neatly.
<?php
$price = 1999.95;
echo "$" . number_format($price, 2);
// Output: $1,999.95
?>
✅ Explanation:
We prefixed the number with a dollar sign $
and formatted it with 2 decimals for currency display.
🔹 Example 5: No Thousand Separator
If you want to format numbers with decimals but without any thousand separators:
<?php
echo number_format(1234567.891, 2, ".", "");
// Output: 1234567.89
?>
✅ Explanation:
We used an empty string ""
for the thousand separator, removing commas or dots between digits.
🔹 Example 6: Large Numbers and Readability
When working with big data, readability matters.
<?php
echo number_format(9876543210, 0);
// Output: 9,876,543,210
?>
✅ Explanation:
Even without decimals, the function ensures the number is easy to read with proper grouping.
🔹 Common Use Cases of number_format()
- E-commerce Websites → Displaying product prices in a clean format.
- Financial Applications → Formatting salary, tax, or loan calculations.
- Statistical Dashboards → Showing large datasets with commas for clarity.
- Localized Number Formatting → Adapting numbers for different regions.
🔹 Best Practices for Using number_format()
- Always define decimal precision when formatting currency values.
- Use the correct decimal and thousand separators based on your target audience’s region.
- When handling mathematical operations, use raw numbers in calculations and apply
number_format()
only at the display level.
🔹 Frequently Asked Questions (FAQ)
What does number_format() return?
The function always returns a string, not a float.
Does number_format() round numbers?
Yes, it rounds numbers to the nearest decimal place specified.
Can I use number_format() for percentages?
Yes. You can format percentages the same way as numbers, then append a %
sign.
echo number_format(0.12345 * 100, 2) . "%";
// Output: 12.35%
Is number_format() locale-aware?
No. For locale-based formatting, use numberformatter
class from PHP’s intl
extension.
✅ Conclusion
The PHP number_format()
function is a simple yet powerful way to make numbers more readable and user-friendly. Whether you are formatting prices, percentages, or large numbers, this function ensures professional-looking output.
By customizing decimals, separators, and symbols, you can adapt it for different regions, industries, and applications. Use it wisely in your PHP projects, and you’ll significantly improve user experience.