Content · Programming · PHP

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

Learn how to create a locale-aware number formatter 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 create a locale-aware number formatter. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

NumberFormatter::create: The style determines whether values are formatted as decimal, percent, currency or another ICU number form.

What Is NumberFormatter::create in PHP?

Create a locale-aware number formatter. The style determines whether values are formatted as decimal, percent, currency or another ICU number form.

Syntax:

NumberFormatter::create(string $locale, int $style, ?string $pattern = null): ?NumberFormatter

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: Invalid styles or patterns can fail. Locale-specific spacing and separators can change with ICU versions.

Example 1: Basic Usage

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

<?php

var_dump(NumberFormatter::create("en_US", NumberFormatter::DECIMAL) instanceof NumberFormatter);

The code focuses on the main operation. The style determines whether values are formatted as decimal, percent, currency or another ICU number form.

Example 2: Another Case

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

<?php

$formatter = NumberFormatter::create("de_DE", NumberFormatter::DECIMAL);
echo $formatter->format(1234.5);

What changes? Invalid styles or patterns can fail. Locale-specific spacing and separators can change with ICU versions.

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

$formatter = NumberFormatter::create("fr_FR", NumberFormatter::PERCENT);
if ($formatter !== null) {
    echo $formatter->format(0.25);
}

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. The style determines whether values are formatted as decimal, percent, currency or another ICU number form.

Important Behavior and Edge Cases

  • Core behavior: The style determines whether values are formatted as decimal, percent, currency or another ICU number form.
  • Inputs and results: Invalid styles or patterns can fail. Locale-specific spacing and separators can change with ICU versions.
  • Alternatives: NumberFormatter::format() formats values using the object returned by create().
  • Compatibility: The intl extension is required.

Comparison With Related Functions

Function or ApproachWhen to Use It
NumberFormatter::createCreate a locale-aware number formatter.
Related PHP approachNumberFormatter::format() formats values using the object returned by create().

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionInvalid styles or patterns can fail. Locale-specific spacing and separators can change with ICU versions.
Confusing this API with a related operationNumberFormatter::format() formats values using the object returned by create().
Assuming output is identical across environmentsCheck return values and required extensions or versions. The intl extension is required.

Key Takeaways

  • NumberFormatter::create helps you create a locale-aware number formatter.
  • The style determines whether values are formatted as decimal, percent, currency or another ICU number form.
  • Invalid styles or patterns can fail. Locale-specific spacing and separators can change with ICU versions.

Use NumberFormatter::create when you need to create a locale-aware number formatter. 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.