Skip to content
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

[11.x] Add a new Number::scientific() method to format numbers in E-notation #54451

Open
wants to merge 6 commits into
base: 11.x
Choose a base branch
from

Conversation

waadmawlood
Copy link

@waadmawlood waadmawlood commented Feb 3, 2025

Add scientific notation formatting to the Number class

This PR adds a new scientific() method to the Number class that formats numbers in scientific notation using PHP's Intl extension.

The new method allows formatting numbers in E-notation with configurable precision and locale support. For example:

// Syntax
scientific(int|float $number, int $exponent = null, int $precision = 2, ?string $locale = null)

Number::scientific(12.34); // "1.23E1"
Number::scientific(12.34, exponent: 2); // "0.12E2"
Number::scientific(1234567); // "1.23E6"
Number::scientific(1234567, exponent: 5); // "12.35E5"
Number::scientific(0.00123); // "1.23E-3"
Number::scientific(0.00823, exponent: -3); // "8.23E-3'"
Number::scientific(12.34, precision: 4); // "1.234E1"
Number::scientific(12.34, precision: 2, locale: 'fr'); // "1,23E1"

Number::scientific(-12.34)); // "-1.23E1"
Number::scientific(-1, exponent: -1); // "-10E-1"

Number::scientific(INF) // "∞"
Number::scientific(NAN)); // "NaN"
Number::scientific(NAN, locale: 'fr')); // "NaN"
Number::scientific(NAN, locale: 'ru')); // "не число"
Number::scientific(NAN, locale: 'ar')); // "ليس رقمًا"

Key features:

  • Supports positive and negative numbers
  • Locale-aware formatting
  • Handles special values (INF, NAN)
  • Requires the PHP Intl extension

This addition complements the existing number formatting capabilities in Laravel's Number class by providing scientific notation support for working with very large or small numbers in a standardized way.

@waadmawlood waadmawlood changed the title add A new scientific() method to format numbers in E-notation. Add a new scientific() method to format numbers in E-notation. Feb 3, 2025
@shaedrich
Copy link
Contributor

How about adding an exponent:

Number::scientific(1234567); // "1.23E6"
Number::scientific(1234567, exponent: 5); // "12.3E5"

@waadmawlood
Copy link
Author

How about adding an exponent:

Number::scientific(1234567); // "1.23E6"
Number::scientific(1234567, exponent: 5); // "12.3E5"

Thanks for the suggestion!

I'm not sure if the logic is correct or not because the method is formatting not converting.
I think the correct thing is to convert the number to the exponent you want to format and then formatting the new number.
In the method, it has the precision of number.

// syntax
scientific(int|float $number, int $precision = 2, ?string $locale = null)

But if this was necessary, so we could be added it later.

@shaedrich
Copy link
Contributor

shaedrich commented Feb 3, 2025

How is this formatting if you randomly choose the exponent with no way to change that. The exponent is chosen either way, the only difference being that I proposed to make it customizable.

Both 1.23E6 and 12.3E5 represent the same number.

When you choose to randomly select an exponent, then you could take the exponent that doesn't cut off numbers with the given precision as 1.23E6 converts back to 1230000, not 1234567.

@waadmawlood
Copy link
Author

Thanks for the clarification, I understood your point exactly and it is indeed correct.

@shaedrich
Copy link
Contributor

shaedrich commented Feb 3, 2025

Next thing to keep in mind is that "scientific notation" can stand for different notations:

I came up with the same feature a while ago and didn't have the time to finish it yet. Might be helpful to you:
https://github.com/laravel/framework/compare/11.x...shaedrich:framework:codespace-legendary-garbanzo-pjr5v5w75v535qv?expand=1

@waadmawlood
Copy link
Author

@shaedrich I improved code and raised again. last commit 885a879 4ba5350

@shaedrich
Copy link
Contributor

shaedrich commented Feb 3, 2025

@waadmawlood Your variable wording is definitely better than mine—one never stops learning 😅👍🏻

@crynobone crynobone changed the title Add a new scientific() method to format numbers in E-notation. [11.x] Add a new Number::scientific() method to format numbers in E-notation. Feb 4, 2025
@crynobone crynobone changed the title [11.x] Add a new Number::scientific() method to format numbers in E-notation. [11.x] Add a new Number::scientific() method to format numbers in E-notation Feb 4, 2025
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.

2 participants