Skip to content

Fix toCurrency to correctly strip currency sign #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions library/Zend/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -232,7 +231,7 @@ public function toCurrency($value = null, array $options = array())

default:
$sign = '';
$value = str_replace(' ', '', $value);
$value = $this->_stripCurrencyPattern($value);
break;
}
}
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/Zend/CurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down