Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Monetary Formatting

Monetary formatting is the locale-aware presentation of a numeric value as a currency amount, correctly choosing the currency symbol, its position before or after the number, the decimal and thousands separators, and the number of decimal places for the target locale and currency; the same value of 1234.56 should display as $1,234.56 in the United States and as 1 234,56 € in France. PHP originally handled this through setlocale(LC_MONETARY, ...) together with localeconv(), which exposes the current locale’s monetary conventions as an array, and the dedicated money_format() function, but money_format() was deprecated in version 7.4 and removed entirely in version 8.0. The modern replacement is the NumberFormatter class from the intl extension, constructed with a locale and the NumberFormatter::CURRENCY style, whose formatCurrency() method produces a correctly formatted string for any given locale and ISO 4217 currency code, and whose companion parseCurrency() method can parse a formatted string back into a numeric value and currency code. Because formatting money incorrectly is an easy and visible mistake in international applications, using NumberFormatter rather than manual string concatenation is the recommended approach in current PHP code.

<?php

    $fmt = new NumberFormatter('fr_FR', NumberFormatter::CURRENCY);
    echo $fmt->formatCurrency(1234.56, 'EUR');
    // 1 234,56 €
    
    $fmt = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
    echo $fmt->formatCurrency(1234.56, 'USD');
    // $1,234.56

?>

Documentation

See Also