[12.x] Add locale-aware number parsing methods to Number class #55725
+76
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add locale-aware number parsing methods
This PR adds three new methods to the Number class to complement the existing number formatting capabilities:
Number::parse(string $string, ?int $type = NumberFormatter::TYPE_DOUBLE, ?string $locale = null)
: Parses a localized number string into its numeric valueNumber::parseInt(string $string, ?string $locale = null)
: Specifically parses a string into an integer with locale supportNumber::parseFloat(string $string, ?string $locale = null)
: Specifically parses a string into a float with locale supportUsage
Currently, I need to create something like
App\Helpers\NumberFormatter::parse($request->input('amount'))
or using directly
(new NumberFormatter(config('app.locale'), NumberFormatter::DECIMAL)) ->parse($request->input('number'));
to parse string generated by
Number::format()
.For eg:
Why should the key to start a car engine be different from the key to turn the engine off ?
Why not this ?