Content · Programming · PHP

PHP NumberFormatter::formatCurrency() Method: Usage and Examples

Learn how to format a number as a localized currency amount in PHP with syntax, clear code examples, practical use, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When working with PHP, you may need to format a number as a localized currency amount. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

NumberFormatter::formatCurrency: Currency identifiers such as USD and EUR are separate from locale settings.

What Is NumberFormatter::formatCurrency in PHP?

Format a number as a localized currency amount. Currency identifiers such as USD and EUR are separate from locale settings.

Syntax:

$formatter->formatCurrency(float $amount, string $currency): string|false

Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.

Important: Formatting does not convert exchange rates. Floating-point amounts need careful treatment for financial calculations.

Example 1: Basic Usage

This example introduces the operation with a short and predictable input.

<?php

$formatter = new NumberFormatter("en_US", NumberFormatter::CURRENCY);
echo $formatter->formatCurrency(99.95, "USD");

The code focuses on the main operation. Currency identifiers such as USD and EUR are separate from locale settings.

Example 2: Another Case

Now compare a different input, locale, date, or option to see another part of the behavior.

<?php

$formatter = new NumberFormatter("de_DE", NumberFormatter::CURRENCY);
echo $formatter->formatCurrency(99.95, "EUR");

What changes? Formatting does not convert exchange rates. Floating-point amounts need careful treatment for financial calculations.

Practical Example: Applying It in an Application

Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.

<?php

$prices = [12.5, 18.99, 42];
$formatter = new NumberFormatter("en_US", NumberFormatter::CURRENCY);
foreach ($prices as $price) {
    echo $formatter->formatCurrency($price, "USD") . PHP_EOL;
}

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Currency identifiers such as USD and EUR are separate from locale settings.

Important Behavior and Edge Cases

  • Core behavior: Currency identifiers such as USD and EUR are separate from locale settings.
  • Inputs and results: Formatting does not convert exchange rates. Floating-point amounts need careful treatment for financial calculations.
  • Alternatives: NumberFormatter::format() handles ordinary numbers; formatCurrency() adds currency rules.
  • Compatibility: The intl extension is required.

Comparison With Related Functions

Function or ApproachWhen to Use It
NumberFormatter::formatCurrencyFormat a number as a localized currency amount.
Related PHP approachNumberFormatter::format() handles ordinary numbers; formatCurrency() adds currency rules.

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionFormatting does not convert exchange rates. Floating-point amounts need careful treatment for financial calculations.
Confusing this API with a related operationNumberFormatter::format() handles ordinary numbers; formatCurrency() adds currency rules.
Assuming output is identical across environmentsCheck return values and required extensions or versions. The intl extension is required.

Key Takeaways

  • NumberFormatter::formatCurrency helps you format a number as a localized currency amount.
  • Currency identifiers such as USD and EUR are separate from locale settings.
  • Formatting does not convert exchange rates. Floating-point amounts need careful treatment for financial calculations.

Use NumberFormatter::formatCurrency when you need to format a number as a localized currency amount. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.

Share this resourceSend this page to someone who may find it useful.