Skip to content

[11.x] feat: introduce option to change default Number currency#53022

Merged
taylorotwell merged 8 commits intolaravel:11.xfrom
sts-ryan-holton:feature/number_currency_settings
Oct 4, 2024
Merged

[11.x] feat: introduce option to change default Number currency#53022
taylorotwell merged 8 commits intolaravel:11.xfrom
sts-ryan-holton:feature/number_currency_settings

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
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