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

@ -110,6 +110,14 @@ 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'])) {
@ -122,13 +130,22 @@ class NumberHelper extends AppHelper {
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.
@ -142,13 +159,13 @@ class NumberHelper extends AppHelper {
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() {