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] feat: introduce option to change default Number currency #53022

Merged

Conversation

sts-ryan-holton
Copy link
Contributor

Hi 👋,

This PR introduces the functionality to globally change the default currency of the Number helper, much like the locale. Right now the default is passed aa a function argument, however, it can be useful to change the global currency.

The use case here is that the default currency is still USD for backwards compatibility with applications, and the $in variable second arg is still retained.

I've introduced a $currency variable, defaulted to US.

Comment on lines +125 to +131
public static function currency(int|float $number, string $in = '', ?string $locale = null)
{
static::ensureIntlExtensionIsInstalled();

$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::CURRENCY);

return $formatter->formatCurrency($number, $in);
return $formatter->formatCurrency($number, ! empty($in) ? $in : static::$currency);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would handle it the same as the $locale-parameter:

    public static function currency(int|float $number, ?string $in = null, ?string $locale = null)
    {
        static::ensureIntlExtensionIsInstalled();

        $formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::CURRENCY);

        return $formatter->formatCurrency($number, $in ?? static::$currency);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with ! empty() in case $in was passed as empty :)

@taylorotwell taylorotwell merged commit 2747a5f into laravel:11.x Oct 4, 2024
33 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