Skip to content

feat: Allow custom precision in Number::currency() #54848

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

Closed
wants to merge 1 commit into from

Conversation

philharmonie
Copy link

This update enhances the Number::currency() method by introducing an optional $precision parameter, allowing developers to specify the number of decimal places when formatting currency values.

Before vs. After:

Previously, the method always used the default precision for each currency, which was sometimes unnecessary or inconsistent with application requirements:

Number::currency(1234.56, in: 'EUR'); // Outputs: "1.234,56 €"
Number::currency(100, in: 'JPY'); // Outputs: "¥100" (Japanese Yen has no decimals by default)

After this update:
Developers can now explicitly set the precision when needed:

Number::currency(1234.56, in: 'EUR', precision: 0); // Outputs: "1.235 €"
Number::currency(1234.56, in: 'EUR', precision: 2); // Outputs: "1.234,56 €"
Number::currency(1234.56, in: 'EUR', precision: 3); // Outputs: "1.234,560 €"
Number::currency(100, in: 'JPY', precision: 2); // Outputs: "¥100.00"

Benefits to End Users

  • Greater flexibility: Developers can now control how currency values are displayed, instead of relying on the formatter’s default precision.
  • Better user experience: In some cases, financial applications or reporting tools require rounded values (e.g., whole numbers for invoices).
  • Consistency across locales and currencies: Some currencies, like JPY, default to zero decimals, while others like USD default to two. Now, developers can standardize their output.

Why It Doesn't Break Existing Features

  • Backward compatibility: If no precision is provided, the method continues to use the default currency-specific precision.
  • No API changes: The method signature remains the same, with an optional additional parameter.
  • Relies on NumberFormatter: The change does not override any locale-specific behavior unless explicitly requested by the developer.

How It Makes Web Development Easier

  • Developers no longer need to manually round numbers before passing them to currency(), reducing boilerplate code.
  • Web applications that deal with multiple currencies (e.g., e-commerce platforms, finance dashboards) can now enforce consistent display formats without additional logic.
  • Works seamlessly with Laravel's internationalization features, making it easier to present localized currency formats without surprises.

This update enhances the `Number::currency()` method by introducing an optional `$precision` parameter. 
Users can now specify the number of decimal places when formatting currency values.

Previously, the method always used the default precision for each currency. With this change, developers 
can override this behavior by passing a specific precision value.

Example usage:

```php
Number::currency(1234.56, 'EUR', null, 0); // Outputs: "1.235 €"
Number::currency(1234.56, 'EUR', null, 2); // Outputs: "1.234,56 €"
```
@shaedrich
Copy link
Contributor

This really is a popular topic 😂 But hasn't this already been done in #54456?

@crynobone crynobone closed this Feb 28, 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.

3 participants