Change the Default Currency of the Laravel Number Helper

October 15th, 2024 • 1 minute read time

If you're using the Number helper in Laravel to format currency, there's a quick and easy global way to set the default currency.

Here's what you'd have to do previously:

Number::currency(1000, 'GBP'); // £1,000

Pretty annoying if you're using a different currency than the default USD!

Here's how to set the default currency for the Number helper:

Number::useCurrency('GBP');

Any calls to Number::currency after this will use the currency you've set here.

If you're only working with a single currency, or the currency is dependent on a stored value (e.g. the user), a great place to put this is in your AppServiceProvider so you can forget about it:

<?php
use Illuminate\Support\Number;

class AppServiceProvider extends ServiceProvider
{
    //...

    public function boot(): void
    {
        Number::useCurrency('EUR');
    }
}

Easy! You can now format currencies without manually specifying the currency every time.

If you found this article helpful, you'll love our practical screencasts.
Author
Alex Garrett-Smith
Share :

Comments

No comments, yet. Be the first!

Tagged under