8000 [12.x] Add locale-aware number parsing methods to Number class by informagenie · Pull Request #55725 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Add locale-aware number parsing methods to Number class #55725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

informagenie
Copy link
Contributor
@informagenie informagenie commented May 13, 2025

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 value
  • Number::parseInt(string $string, ?string $locale = null): Specifically parses a string into an integer with locale support
  • Number::parseFloat(string $string, ?string $locale = null): Specifically parses a string into a float with locale support

Usage

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:

$number = 12345.67;
$string = Number::format($number, locale: 'en'); //12,345.67
$string = Number::format($number, locale: 'de'); //12.345,67
$string = Number::format($number, locale: 'fr'); //12 345,67

$float = App\Helpers\NumberFormatter::parse($request->input($string));

Why should the key to start a car engine be different from the key to turn the engine off ?

Why not this ?

$number = 12345.67;
$string = Number::format($number, locale: 'en'); //12,345.67
$string = Number::format($number, locale: 'de'); //12.345,67
$string = Number::format($number, locale: 'fr'); //12 345,67

$float = Number::parse($request->input($string));

Add three new methods to the Number class for parsing numeric strings:
- parse(): Parse string to number based on format type
- parseInt(): Parse string to integer with locale support
- parseFloat(): Parse string to float with locale support

These methods leverage the PHP Intl extension's NumberFormatter to provide
locale-aware number parsing capabilities.
Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@informagenie informagenie marked this pull request as ready for review May 13, 2025 16:42
@shaedrich
Copy link
Contributor
  • ?int $type = NumberFormatter::TYPE_DOUBLE
  • Number::parseInt
  • Number::parseFloat

Why would you need that? If you need to cut off the decimal places, this should be ideally done in a two-step operation by first parsing it and then passing it to round() or the like

@informagenie
Copy link
Contributor Author

Why would you need that? If you need to cut off the decimal places, this should be ideally done in a two-step operation by first parsing it and then passing it to round() or the like

🤔...

Honestly, for just straightforward code.

Do you think just Number::parse(string $string, ?string $locale = null) will be enough ?

@shaedrich
Copy link
Contributor

Do you think just Number::parse(string $string, ?string $locale = null) will be enough ?

Yep 👍🏻

@taylorotwell taylorotwell merged commit 5c146fb into laravel:12.x May 13, 2025
60 of 61 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0