number_format() & money_format()
Also Known As
number_format
money_format
PHP number formatting
PHP currency format
TL;DR
number_format() formats a number with grouped thousands and a specified number of decimal places — the correct way to display prices, statistics, and large integers in PHP. money_format() was deprecated in PHP 7.4 and removed in PHP 8.0.
Explanation
number_format(float $num, int $decimals, string $decimal_separator, string $thousands_separator) converts a float to a formatted string. The defaults produce English-style formatting (1,234.56), but all separators are configurable for locale-specific output (1.234,56 for German). money_format() was a thin wrapper around the C library strfmon() — locale-dependent, not available on Windows, and removed in PHP 8.0. The modern replacement is NumberFormatter from the Intl extension, which handles currency symbols, locale rules, and sign conventions correctly. For simple price display without Intl, number_format() with explicit separators is safe and predictable.
Common Misconception
✗ number_format() is locale-aware. It is not — it uses whatever separators you pass explicitly. For locale-correct output (currency symbols, right-to-left numbers, Arabic-Indic digits), use NumberFormatter from the Intl extension.
Why It Matters
Displaying raw floats to users (echo 1234.5) produces output missing thousands separators and trailing zeros — '1234.5' instead of '1,234.50'. number_format() is the one-liner fix. Knowing that money_format() was removed in PHP 8.0 prevents broken upgrades.
Common Mistakes
- Using money_format() in code targeting PHP 8+ — it was removed; replace with NumberFormatter::formatCurrency() or number_format() with explicit separators.
- Storing number_format() output in a database — it adds commas and periods that break numeric operations; always store raw numbers, format only for display.
- Using number_format() for locale-sensitive output without configuring separators — the defaults are English-style regardless of server locale.
- Passing a string to number_format() — it silently coerces to float, which can lose precision for large integers; use intl NumberFormatter for arbitrary precision.
- Formatting floats for financial calculations — floats have precision errors; use bcmath or integer cents and format only at display time.
Code Examples
✗ Vulnerable
<?php
// ❌ Raw float output — no thousands separator, inconsistent decimals
$price = 1234.5;
echo $price; // '1234.5' — missing separator, missing trailing zero
echo round($price, 2); // '1234.5' — still no formatting
// ❌ money_format() — removed in PHP 8.0
echo money_format('%.2n', $price); // Fatal error on PHP 8+
✓ Fixed
<?php
// ✅ number_format() — simple, predictable
$price = 1234.5;
echo number_format($price, 2); // '1,234.50'
echo number_format($price, 2, ',', '.'); // '1.234,50' — German style
echo '$' . number_format($price, 2); // '$1,234.50'
// ✅ NumberFormatter (Intl) — locale-correct currency
$fmt = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
echo $fmt->formatCurrency(1234.5, 'USD'); // '$1,234.50'
$fmt_de = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
echo $fmt_de->formatCurrency(1234.5, 'EUR'); // '1.234,50 €'
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Edited
18 Apr 2026
Views
21
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 6
Perplexity 3
Google 2
ChatGPT 1
Meta AI 1
Ahrefs 1
Also referenced
How they use it
crawler 13
crawler_json 1
⚡
DEV INTEL
Tools & Severity
⚙ Fix effort: Low
⚡ Quick Fix
Replace echo $price with echo number_format($price, 2) for '1,234.50'. Replace any money_format() calls with NumberFormatter or number_format() with explicit separators.
📦 Applies To
PHP 4.0+
web
cli
🔗 Prerequisites