W3docs

money_format()

Our article is about the PHP function money_format(), which is used to format a number as a currency string. This function is useful for working with numbers

The money_format() function formats a number as a currency string, applying a thousands separator, a decimal point, and a currency symbol according to the active locale.

Important: money_format() is deprecated as of PHP 7.4 and removed in PHP 8.0. It also never existed on Windows. If you are writing new code, skip straight to the Modern replacement section below — the rest of this page documents the legacy function so you can read and maintain older PHP code.

Syntax

string money_format ( string $format , float $number )

The function takes two parameters:

  • $format – a string describing how to format the number. It contains one or more conversion specifications (each starting with %), much like sprintf(). Any text outside a specification is printed verbatim.
  • $number – the numeric value to format.

It returns the formatted string.

Common format specifiers

SpecifierMeaningExample output (en_US, 1234.56)
%nNational (local) currency format$1,234.56
%iInternational currency formatUSD 1,234.56
%.2nNational format, 2 decimal digits$1,234.56
%(nWrap negative amounts in parentheses($1,234.56)
%!nSuppress the currency symbol1,234.56

The locale (set with setlocale()) decides which currency symbol, grouping character, and decimal point are used.

Example

<?php
$number = 1234.56;
setlocale(LC_MONETARY, 'en_US');
echo money_format('%n', $number);
?>

Here $number holds a floating-point value, and the setlocale() call configures the monetary locale to en_US. The output is:

$1,234.56

Gotcha: setlocale() returns false if the requested locale is not installed on the server, and money_format() then silently falls back to a default. Always check that the locale name (for example en_US vs. en_US.UTF-8) actually exists on your system.

Modern replacement: NumberFormatter

Because money_format() is gone in PHP 8, the recommended approach is the NumberFormatter class from the intl extension. It is locale-aware, works on every platform (including Windows), and uses ICU data rather than the system locale:

<?php
$number = 1234.56;

$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
echo $formatter->formatCurrency($number, 'USD');
?>

Output:

$1,234.56

To format the same amount for a different locale and currency, just change the constructor locale and the currency code:

<?php
$number = 1234.56;

$de = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
echo $de->formatCurrency($number, 'EUR');
?>

Output (German grouping uses a dot for thousands and a comma for decimals):

1.234,56 €

When to use what

  • New code: use NumberFormatter::formatCurrency() — it is the only currency formatter available in PHP 8+.
  • Legacy code on PHP 7.4 or earlier: money_format() still works but will emit a deprecation notice; plan to migrate.
  • Simple, non-currency numbers: if you only need a thousands separator and fixed decimals (no currency symbol or locale rules), number_format() is lighter weight.
  • number_format() – format a number with grouped thousands.
  • setlocale() – set the locale used by money_format() and other locale-sensitive functions.
  • sprintf() / printf() – general-purpose string formatting.
  • round() – round a value before formatting it as money.

Practice

Practice
What does the 'money_format' function in PHP do?
What does the 'money_format' function in PHP do?
Was this page helpful?