Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #73 from tworzenieweb/master
Browse files Browse the repository at this point in the history
#72 ICU workaround to support formatting currencies different than th…
  • Loading branch information
weierophinney committed May 17, 2017
2 parents 56d907c + 874854f commit 76f5e98
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/View/Helper/CurrencyFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}
2 changes: 2 additions & 0 deletions test/View/Helper/CurrencyFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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'],
];
}

Expand Down

0 comments on commit 76f5e98

Please sign in to comment.