When working with PHP, you may need to convert locale-formatted text into a number. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
NumberFormatter::parse: Decimal symbols and grouping separators depend on the formatter locale.
What Is NumberFormatter::parse in PHP?
Convert locale-formatted text into a number. Decimal symbols and grouping separators depend on the formatter locale.
Syntax:
$formatter->parse(string $string, int $type = NumberFormatter::TYPE_DOUBLE, ?int &$offset = null): int|float|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: Parsing can accept a prefix of the input. Validate full input consumption where strict validation is required.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
$formatter = new NumberFormatter("de_DE", NumberFormatter::DECIMAL);
var_dump($formatter->parse("1.234,50"));
The code focuses on the main operation. Decimal symbols and grouping separators depend on the formatter locale.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
$formatter = new NumberFormatter("en_US", NumberFormatter::DECIMAL);
var_dump($formatter->parse("1,234.50"));
What changes? Parsing can accept a prefix of the input. Validate full input consumption where strict validation is required.
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::DECIMAL);
$rawValues = ["1,200", "2,500", "invalid"];
foreach ($rawValues as $rawValue) {
var_dump($formatter->parse($rawValue));
}
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. Decimal symbols and grouping separators depend on the formatter locale.
Important Behavior and Edge Cases
- Core behavior: Decimal symbols and grouping separators depend on the formatter locale.
- Inputs and results: Parsing can accept a prefix of the input. Validate full input consumption where strict validation is required.
- Alternatives: NumberFormatter::format() converts numbers to localized strings.
- Compatibility: The intl extension is required.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
NumberFormatter::parse | Convert locale-formatted text into a number. |
| Related PHP approach | NumberFormatter::format() converts numbers to localized strings. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | Parsing can accept a prefix of the input. Validate full input consumption where strict validation is required. |
| Confusing this API with a related operation | NumberFormatter::format() converts numbers to localized strings. |
| Assuming output is identical across environments | Check return values and required extensions or versions. The intl extension is required. |
Key Takeaways
- NumberFormatter::parse helps you convert locale-formatted text into a number.
- Decimal symbols and grouping separators depend on the formatter locale.
- Parsing can accept a prefix of the input. Validate full input consumption where strict validation is required.
Use NumberFormatter::parse when you need to convert locale-formatted text into a number. 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.