Fixing issue #2713, NumberHelper does not handle some currency formatting very well

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5324 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mariano.iglesias 2007-06-21 16:01:11 +00:00
parent b870ea170c
commit 97a10184f3
2 changed files with 42 additions and 13 deletions

View file

@ -99,9 +99,9 @@ class NumberHelper extends AppHelper {
if (is_int($options)) { if (is_int($options)) {
$places = $options; $places = $options;
} }
$seperators = array(',', '.', '-', ':'); $seperators = array(',', '.', '-', ':');
$before = null; $before = null;
if (is_string($options) && !in_array( $options, $seperators)) { if (is_string($options) && !in_array( $options, $seperators)) {
$before = $options; $before = $options;
@ -110,26 +110,43 @@ class NumberHelper extends AppHelper {
if (!is_array($options) && in_array( $options, $seperators)) { if (!is_array($options) && in_array( $options, $seperators)) {
$seperator = $options; $seperator = $options;
} }
$decimals = '.';
if(!is_array($options) && in_array( $options, $seperators)) {
$decimals = $options;
}
$escape = true;
if (is_array($options) && isset($options['escape'])) {
$escape = $options['escape'];
}
if (is_array($options)) { if (is_array($options)) {
if (isset($options['places'])) { if (isset($options['places'])) {
$places = $options['places']; $places = $options['places'];
unset($options['places']); unset($options['places']);
} }
if (isset($options['before'])) { if (isset($options['before'])) {
$before = $options['before']; $before = $options['before'];
unset($options['before']); unset($options['before']);
} }
if(isset($options['decimals'])) {
$decimals = $options['decimals'];
unset($options['decimals']);
}
if (isset($options['seperator'])) { if (isset($options['seperator'])) {
$seperator = $options['seperator']; $seperator = $options['seperator'];
unset($options['seperator']); unset($options['seperator']);
} }
} }
return h($before) . number_format ($number, $places, ".", $seperator); if ($escape) {
} $before = h($before);
}
return $before . number_format ($number, $places, $decimals, $seperator);
}
/** /**
* Formats a number into a currency format. * Formats a number into a currency format.
* *
@ -139,16 +156,16 @@ class NumberHelper extends AppHelper {
* @static * @static
*/ */
function currency ($number, $currency = 'USD') { function currency ($number, $currency = 'USD') {
switch ($currency) { switch ($currency) {
case "EUR": case "EUR":
return $this->format($number, array('places'=>'2', 'before'=>"&#128")); return $this->format($number, array('escape' => false, 'places'=>'2', 'before'=>'€', 'seperator'=>'.', 'decimals'=>','));
break; break;
case "GBP": case "GBP":
return $this->format($number, array('places'=>'2', 'before'=>"&#163")); return $this->format($number, array('escape' => false, 'places'=>'2', 'before'=>'£'));
break; break;
case 'USD': case 'USD':
return $this->format($number, array('places'=>'2', 'before'=>"$")); return $this->format($number, array('places'=>'2', 'before'=>'$'));
break; break;
default: default:
return $this->format($number, array('places'=>'2', 'before'=> $currency)); return $this->format($number, array('places'=>'2', 'before'=> $currency));

View file

@ -69,6 +69,18 @@ class NumberTest extends UnitTestCase {
$result = $this->Helper->currency($value, false); $result = $this->Helper->currency($value, false);
$expected = '100,100,100.00'; $expected = '100,100,100.00';
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$result = $this->Helper->currency($value, 'USD');
$expected = '$100,100,100.00';
$this->assertEqual($expected, $result);
$result = $this->Helper->currency($value, 'EUR');
$expected = '€100.100.100,00';
$this->assertEqual($expected, $result);
$result = $this->Helper->currency($value, 'GBP');
$expected = '£100,100,100.00';
$this->assertEqual($expected, $result);
} }
function testToReadableSize() { function testToReadableSize() {
@ -76,7 +88,7 @@ class NumberTest extends UnitTestCase {
$expected = '0 Bytes'; $expected = '0 Bytes';
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
} }
function tearDown() { function tearDown() {
unset($this->Helper); unset($this->Helper);
} }