diff --git a/src/View/Helper/CurrencyFormat.php b/src/View/Helper/CurrencyFormat.php index 0ffe1de6..9c91b50e 100644 --- a/src/View/Helper/CurrencyFormat.php +++ b/src/View/Helper/CurrencyFormat.php @@ -54,6 +54,16 @@ class CurrencyFormat extends AbstractHelper */ protected $showDecimals = true; + /** + * Special condition to be checked due to ICU bug: + * http://bugs.icu-project.org/trac/ticket/10997 + * + * @var bool + */ + protected $correctionNeeded = false; + + + /** * @throws Exception\ExtensionNotLoadedException if ext/intl is not present */ @@ -132,11 +142,16 @@ protected function formatCurrency( if ($showDecimals) { $this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 2); + $this->correctionNeeded = false; } else { $this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 0); + $defaultCurrencyCode = $this->formatters[$formatterId]->getTextAttribute(NumberFormatter::CURRENCY_CODE); + $this->correctionNeeded = $defaultCurrencyCode !== $currencyCode; } - return $this->formatters[$formatterId]->formatCurrency($number, $currencyCode); + $formattedNumber = $this->formatters[$formatterId]->formatCurrency($number, $currencyCode); + + return $this->correctionNeeded ? $this->correctICUBug($formattedNumber, $this->formatters[$formatterId]) : $formattedNumber; } /** @@ -230,4 +245,18 @@ public function shouldShowDecimals() { return $this->showDecimals; } + + + + /** + * @param string $formattedNumber + * @param NumberFormatter $formatter + * @return string + */ + private function correctICUBug($formattedNumber, NumberFormatter $formatter) + { + $pattern = sprintf('/\%s\d+$/', $formatter->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL)); + + return preg_replace($pattern, '', $formattedNumber); + } } diff --git a/test/View/Helper/CurrencyFormatTest.php b/test/View/Helper/CurrencyFormatTest.php index af7c1c62..8ecd847d 100644 --- a/test/View/Helper/CurrencyFormatTest.php +++ b/test/View/Helper/CurrencyFormatTest.php @@ -63,6 +63,8 @@ public function currencyProvider() //array('en_US', 'EUR', false, 1234567.891234567890000, null, '€1,234,568'), //array('en_US', 'EUR', false, 1234567.891234567890000, null, '€1,234,568'), ['en_US', 'USD', false, 1234567.891234567890000, null, '$1,234,568'], + /* @see http://bugs.icu-project.org/trac/ticket/10997 */ + ['en_US', 'EUR', false, 1234567.891234567890000, null, '€1,234,567'], ]; }