mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
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:
parent
b870ea170c
commit
97a10184f3
2 changed files with 42 additions and 13 deletions
|
@ -99,9 +99,9 @@ class NumberHelper extends AppHelper {
|
|||
if (is_int($options)) {
|
||||
$places = $options;
|
||||
}
|
||||
|
||||
|
||||
$seperators = array(',', '.', '-', ':');
|
||||
|
||||
|
||||
$before = null;
|
||||
if (is_string($options) && !in_array( $options, $seperators)) {
|
||||
$before = $options;
|
||||
|
@ -110,26 +110,43 @@ class NumberHelper extends AppHelper {
|
|||
if (!is_array($options) && in_array( $options, $seperators)) {
|
||||
$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 (isset($options['places'])) {
|
||||
$places = $options['places'];
|
||||
unset($options['places']);
|
||||
}
|
||||
|
||||
|
||||
if (isset($options['before'])) {
|
||||
$before = $options['before'];
|
||||
unset($options['before']);
|
||||
}
|
||||
|
||||
|
||||
if(isset($options['decimals'])) {
|
||||
$decimals = $options['decimals'];
|
||||
unset($options['decimals']);
|
||||
}
|
||||
|
||||
if (isset($options['seperator'])) {
|
||||
$seperator = $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.
|
||||
*
|
||||
|
@ -139,16 +156,16 @@ class NumberHelper extends AppHelper {
|
|||
* @static
|
||||
*/
|
||||
function currency ($number, $currency = 'USD') {
|
||||
|
||||
|
||||
switch ($currency) {
|
||||
case "EUR":
|
||||
return $this->format($number, array('places'=>'2', 'before'=>"€"));
|
||||
return $this->format($number, array('escape' => false, 'places'=>'2', 'before'=>'€', 'seperator'=>'.', 'decimals'=>','));
|
||||
break;
|
||||
case "GBP":
|
||||
return $this->format($number, array('places'=>'2', 'before'=>"£"));
|
||||
return $this->format($number, array('escape' => false, 'places'=>'2', 'before'=>'£'));
|
||||
break;
|
||||
case 'USD':
|
||||
return $this->format($number, array('places'=>'2', 'before'=>"$"));
|
||||
return $this->format($number, array('places'=>'2', 'before'=>'$'));
|
||||
break;
|
||||
default:
|
||||
return $this->format($number, array('places'=>'2', 'before'=> $currency));
|
||||
|
|
|
@ -69,6 +69,18 @@ class NumberTest extends UnitTestCase {
|
|||
$result = $this->Helper->currency($value, false);
|
||||
$expected = '100,100,100.00';
|
||||
$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() {
|
||||
|
@ -76,7 +88,7 @@ class NumberTest extends UnitTestCase {
|
|||
$expected = '0 Bytes';
|
||||
$this->assertEqual($expected, $result);
|
||||
}
|
||||
|
||||
|
||||
function tearDown() {
|
||||
unset($this->Helper);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue