Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP NumberFormatter and Currency, can't set precision

I want to set a precision of 0 when using the NumberFormatter PHP class (from Intl extension) with currency. However I've got some strange result. Here:

$numberFormatter = new NumberFormatter('en-US', NumberFormatter::CURRENCY);
$numberFormatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 0);

echo $numberFormatter->formatCurrency('45', 'USD');

It outputs $45, which is what I want. However, if I change the currency to EUR with the same settings:

echo $numberFormatter->formatCurrency('45', 'EUR');

It outputs €45.00 (although I explicitly set to have a precision of zero).

Even more strange, if I set the locale to fr-FR, it outputs the number as expected:

$numberFormatter = new NumberFormatter('fr-FR', NumberFormatter::CURRENCY);
$numberFormatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 0);

echo $numberFormatter->formatCurrency('45', 'EUR');

It outputs 45 €.

Is this a bug?

like image 405
Michael Gallego Avatar asked Sep 09 '12 12:09

Michael Gallego


1 Answers

Michael Gallego forgot to update this issue. Check out his bug report and the replies at php.net.

  • https://bugs.php.net/bug.php?id=63140
  • http://bugs.icu-project.org/trac/ticket/7667

[2012-10-05 08:21 UTC] jpauli@email.com

I confirm this is an ICU bug in 4.4.x branch.
Consider upgrading libicu, 4.8.x gives correct result

like image 130
LGT Avatar answered Sep 18 '22 19:09

LGT