diff --git a/library/Zend/Currency.php b/library/Zend/Currency.php index 33408c498e..62686aaa54 100644 --- a/library/Zend/Currency.php +++ b/library/Zend/Currency.php @@ -194,12 +194,11 @@ public function toCurrency($value = null, array $options = array()) 'precision' => $options['precision'])); if ($options['position'] !== self::STANDARD) { - $value = str_replace('¤', '', $value); $space = ''; - if (iconv_strpos($value, ' ') !== false) { - $value = str_replace(' ', '', $value); + if (iconv_strpos($value, '¤ ') !== false || iconv_strpos($value, ' ¤') !== false) { $space = ' '; } + $value = $this->_stripCurrencyPattern($value); if ($options['position'] == self::LEFT) { $value = '¤' . $space . $value; @@ -232,7 +231,7 @@ public function toCurrency($value = null, array $options = array()) default: $sign = ''; - $value = str_replace(' ', '', $value); + $value = $this->_stripCurrencyPattern($value); break; } } @@ -241,6 +240,25 @@ public function toCurrency($value = null, array $options = array()) return $value; } + /** + * Strip the currency pattern an any sorrounding + * whitespace from a string value + * + * @param string $value + * @return string + */ + private function _stripCurrencyPattern($value) { + if (iconv_strpos($value, '¤ ') === 0) { + return iconv_substr($value, 2); + } + else if (iconv_strpos($value, ' ¤') === iconv_strlen($value) - 2) { + return iconv_substr($value, 0, iconv_strlen($value) - 2); + } + else { + return str_replace('¤', '', $value); + } + } + /** * Internal method to extract the currency pattern * when a choice is given based on the given value diff --git a/tests/Zend/CurrencyTest.php b/tests/Zend/CurrencyTest.php index adfe8c09ba..2163706a79 100644 --- a/tests/Zend/CurrencyTest.php +++ b/tests/Zend/CurrencyTest.php @@ -303,6 +303,33 @@ public function testToCurrency() $this->assertSame('-₹ 3,00', $INR->toCurrency(-3)); } + /* + * testing space separators + */ + public function testSpaceSeparatorWithNoSymbol() + { + $HUF = new Zend_Currency('HUF','hu_HU'); + $EUR = new Zend_Currency('USD','de_AT'); + $USD = new Zend_Currency('USD','en_US'); + + $options = array( + 'display' => Zend_Currency::NO_SYMBOL + ); + $this->assertSame('53 292,18', $HUF->toCurrency(53292.18, $options)); + $this->assertSame('53.292,18', $EUR->toCurrency(53292.18, $options)); + $this->assertSame('53,292.18', $USD->toCurrency(53292.18, $options)); + + $options['position'] = Zend_Currency::LEFT; + $this->assertSame('53 292,18', $HUF->toCurrency(53292.18, $options)); + $this->assertSame('53.292,18', $EUR->toCurrency(53292.18, $options)); + $this->assertSame('53,292.18', $USD->toCurrency(53292.18, $options)); + + $options['position'] = Zend_Currency::RIGHT; + $this->assertSame('53 292,18', $HUF->toCurrency(53292.18, $options)); + $this->assertSame('53.292,18', $EUR->toCurrency(53292.18, $options)); + $this->assertSame('53,292.18', $USD->toCurrency(53292.18, $options)); + } + /** * testing setFormat *