Yevgeniy Afanasyev
Yevgeniy Afanasyev

Reputation: 41300

How to set currency to setlocale / money_format in PHP

setlocale is taking a country and a language as parameters.

'money_format' is taking amount and other params.

But how can I tell a currency to PHP?

What if I want to use EUR in Australia?

like javascript can:

var formatter = new Intl.NumberFormat('en-AU', {
  style: 'currency',
  currency: 'EUR',
});
formatter.format(2500);

Chrome obviously understands that it is a foreign currency that need to be formatted to international currency format like so "EUR 2,500.00".

IE does not understand it, but it offers something "€2,500.00"

PHP (that is how I use it)

setlocale(LC_MONETARY, 'en-AU');
return money_format('%.2n', 2500);

gives "$2,500.00" (I know it is Ubuntu should be blamed)

It takes some default currency for Australia and I cannot find a way to change it. Is there a formatting library or something that I'm missing that can help?

Java script does not require installing locales individually, like Ubuntu does. Maybe should we rely on Browsers in this case?

Upvotes: 1

Views: 1344

Answers (1)

msg
msg

Reputation: 8161

You can use the NumberFormatter in the intl extension instead.

$fmt = new NumberFormatter('en_AU.UTF8', NumberFormatter::CURRENCY);
print $fmt->formatCurrency(2500, 'EUR');

And it doesn't require to install every locale individually, since it ships with its own locale data.

Upvotes: 1

Related Questions