jwhoami's avatar

How to format currency in laravel

Hi,

In my .env, my locale is set as

APP_LOCALE=es

However, when I format the currency, I get the following 8,00 US$

when I set the locale to 'us', I get the following

$ 8.00

How can I maintain my locale as es and format the number in us format

Thanks

0 likes
8 replies
LaryAI's avatar
Level 58

To format currency in Laravel while maintaining a specific locale, you can use the NumberFormatter class provided by PHP's intl extension. This allows you to format numbers, including currency, according to different locales.

Here's how you can achieve this:

  1. Ensure the intl extension is enabled: Make sure the intl extension is enabled in your PHP configuration. You can check this by running php -m and looking for intl in the list of modules.

  2. Use NumberFormatter to format the currency: You can create a helper function or use it directly in your code to format the currency according to the desired locale.

Here's an example of how you can format the currency in US format while keeping the locale as es:

<?php

function formatCurrency($amount, $locale = 'en_US', $currency = 'USD')
{
    $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
    return $formatter->formatCurrency($amount, $currency);
}

// Example usage
$amount = 8.00;
$formattedCurrency = formatCurrency($amount, 'en_US', 'USD');
echo $formattedCurrency; // Outputs: .00

In this example:

  • The formatCurrency function takes three parameters: the amount to be formatted, the locale, and the currency code.
  • The NumberFormatter class is instantiated with the desired locale (en_US for US format) and the CURRENCY style.
  • The formatCurrency method formats the amount according to the specified locale and currency.

You can call this function whenever you need to format a currency value in your application.

If you want to keep your locale as es in your .env file but format the currency in US format, you can simply use the en_US locale in the formatCurrency function as shown above.

This way, you maintain your application's locale as es but format the currency in the desired format.

I hope this helps! Let me know if you have any further questions.

martinbean's avatar

However, when I format the currency, I get the following 8,00 US$

@jwhoami Maybe show how you’re formatting currencies?

jwhoami's avatar

@martinbean To format, I used the following

Number::currency($state);

However this is one of those things that mysteriously start working without explanation.

To summarize: .env:

  APP_LOCALE=es

AppServiceProvider

  public function boot(): void
  {
    Illuminate\Support\Number::useLocale('us');
  }

In my application:

Illuminate\Support\Number::currency($state)

And now I get the expected result

$ 8.00

1 like
chuck_wood's avatar

Unfortunately this breaks i18n. It comes with "lang/en" and uses the locale string directly in the file path. Should I make "lang/en_US"?

martinbean's avatar

@chuck_wood I don’t really follow? What do the paths of the localization files have to do with formatting currency strings?

Please or to participate in this conversation.