When working with PHP, you may need to format a number using a locale and selected style. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
NumberFormatter::format: The output is localized text, not a machine-readable decimal representation.
What Is NumberFormatter::format in PHP?
Format a number using a locale and selected style. The output is localized text, not a machine-readable decimal representation.
Syntax:
$formatter->format(int|float $num, int $type = NumberFormatter::TYPE_DEFAULT): 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: A percent-style formatter interprets 0.25 as 25%. Do not store locale-formatted output in numeric database fields.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
$formatter = new NumberFormatter("en_US", NumberFormatter::DECIMAL);
echo $formatter->format(12345.67);
The code focuses on the main operation. The output is localized text, not a machine-readable decimal representation.
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::DECIMAL);
echo $formatter->format(12345.67);
What changes? A percent-style formatter interprets 0.25 as 25%. Do not store locale-formatted output in numeric database fields.
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 = new NumberFormatter("en_US", NumberFormatter::PERCENT);
foreach ([0.1, 0.25, 0.5] as $ratio) {
echo $formatter->format($ratio) . PHP_EOL;
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. The output is localized text, not a machine-readable decimal representation.
Important Behavior and Edge Cases
- Core behavior: The output is localized text, not a machine-readable decimal representation.
- Inputs and results: A percent-style formatter interprets 0.25 as 25%. Do not store locale-formatted output in numeric database fields.
- Alternatives: NumberFormatter::parse() converts suitable localized text back into a number.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
NumberFormatter::format | Format a number using a locale and selected style. |
| Related PHP approach | NumberFormatter::parse() converts suitable localized text back into a number. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | A percent-style formatter interprets 0.25 as 25%. Do not store locale-formatted output in numeric database fields. |
| Confusing this API with a related operation | NumberFormatter::parse() converts suitable localized text back into a number. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- NumberFormatter::format helps you format a number using a locale and selected style.
- The output is localized text, not a machine-readable decimal representation.
- A percent-style formatter interprets 0.25 as 25%. Do not store locale-formatted output in numeric database fields.
Use NumberFormatter::format when you need to format a number using a locale and selected style. 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.