Merge pull request #1148 from dereuromark/2.4-currency

Top 6 world currencies - UTF-8
This commit is contained in:
Mark 2013-03-06 15:29:59 -08:00
commit b105318bcc
2 changed files with 92 additions and 46 deletions

View file

@ -81,6 +81,12 @@ class CakeNumberTest extends CakeTestCase {
$result = $this->Number->format($value, array('places' => 1)); $result = $this->Number->format($value, array('places' => 1));
$expected = '$0.0'; $expected = '$0.0';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$value = 1.23;
$options = array('decimals' => ',', 'thousands' => '.', 'before' => '', 'after' => ' €');
$result = $this->Number->format($value, $options);
$expected = '1,23 €';
$this->assertEquals($expected, $result);
} }
/** /**
@ -107,6 +113,10 @@ class CakeNumberTest extends CakeTestCase {
$expected = '[-100,100,100.00]'; $expected = '[-100,100,100.00]';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->formatDelta(-$value, array('before' => '[ ', 'after' => ' ]'));
$expected = '[ -100,100,100.00 ]';
$this->assertEquals($expected, $result);
$value = 0; $value = 0;
$result = $this->Number->formatDelta($value, array('places' => 1, 'before' => '[', 'after' => ']')); $result = $this->Number->formatDelta($value, array('places' => 1, 'before' => '[', 'after' => ']'));
$expected = '[0.0]'; $expected = '[0.0]';
@ -196,19 +206,22 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'EUR'); $result = $this->Number->currency($value, 'EUR');
$expected = '€100.100.100,00'; $expected = '100.100.100,00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'GBP'); $result = $this->Number->currency($value, 'GBP');
$expected = '£100,100,100.00'; $expected = '£100,100,100.00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, '', array('thousands' => ' ', 'wholeSymbol' => '€', 'wholePosition' => 'after', 'decimals' => ',', 'zero' => 'Gratuit')); $options = array('thousands' => ' ', 'wholeSymbol' => 'EUR ', 'wholePosition' => 'before',
$expected = '100 100 100,00€'; 'decimals' => ',', 'zero' => 'Gratuit');
$result = $this->Number->currency($value, '', $options);
$expected = 'EUR 100 100 100,00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency(1000.45, null, array('after' => 'øre', 'before' => 'Kr. ', 'decimals' => ',', 'thousands' => '.')); $options = array('after' => 'øre', 'before' => 'Kr.', 'decimals' => ',', 'thousands' => '.');
$expected = 'Kr. 1.000,45'; $result = $this->Number->currency(1000.45, null, $options);
$expected = 'Kr.1.000,45';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency(0.5, 'USD'); $result = $this->Number->currency(0.5, 'USD');
@ -227,12 +240,15 @@ class CakeNumberTest extends CakeTestCase {
$expected = '1.00 $'; $expected = '1.00 $';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency(0.2, null, array('wholeSymbol' => ' $', 'wholePosition' => 'after', 'fractionSymbol' => 'cents')); $options = array('wholeSymbol' => '$', 'wholePosition' => 'after', 'fractionSymbol' => ' cents');
$expected = '20cents'; $result = $this->Number->currency(0.2, null, $options);
$expected = '20 cents';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency(0.2, null, array('wholeSymbol' => ' $', 'wholePosition' => 'after', 'fractionSymbol' => 'cents', 'fractionPosition' => 'before')); $options = array('wholeSymbol' => '$', 'wholePosition' => 'after', 'fractionSymbol' => 'cents ',
$expected = 'cents20'; 'fractionPosition' => 'before');
$result = $this->Number->currency(0.2, null, $options);
$expected = 'cents 20';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency(311, 'USD', array('wholePosition' => 'after')); $result = $this->Number->currency(311, 'USD', array('wholePosition' => 'after'));
@ -240,31 +256,48 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency(0.2, 'EUR'); $result = $this->Number->currency(0.2, 'EUR');
$expected = '€0,20'; $expected = '0,20';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency(12, null, array('wholeSymbol' => ' dollars', 'wholePosition' => 'after', 'fractionSymbol' => ' cents', 'fractionPosition' => 'after')); $options = array('wholeSymbol' => ' dollars', 'wholePosition' => 'after', 'fractionSymbol' => ' cents',
'fractionPosition' => 'after');
$result = $this->Number->currency(12, null, $options);
$expected = '12.00 dollars'; $expected = '12.00 dollars';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency(0.12, null, array('wholeSymbol' => ' dollars', 'wholePosition' => 'after', 'fractionSymbol' => ' cents', 'fractionPosition' => 'after')); $options = array('wholeSymbol' => ' dollars', 'wholePosition' => 'after', 'fractionSymbol' => ' cents',
'fractionPosition' => 'after');
$result = $this->Number->currency(0.12, null, $options);
$expected = '12 cents'; $expected = '12 cents';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency(0.5, null, array('fractionSymbol' => false, 'fractionPosition' => 'before', 'wholeSymbol' => '$')); $options = array('fractionSymbol' => false, 'fractionPosition' => 'before', 'wholeSymbol' => '$');
$result = $this->Number->currency(0.5, null, $options);
$expected = '$0.50'; $expected = '$0.50';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency(0, 'GBP'); $result = $this->Number->currency(0, 'GBP');
$expected = '£0.00'; $expected = '£0.00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency(0.00000, 'GBP'); $result = $this->Number->currency(0.00000, 'GBP');
$expected = '£0.00'; $expected = '£0.00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency('0.00000', 'GBP'); $result = $this->Number->currency('0.00000', 'GBP');
$expected = '£0.00'; $expected = '£0.00';
$this->assertEquals($expected, $result);
$result = $this->Number->currency('-2.23300', 'JPY');
$expected = '(¥2.23)';
$this->assertEquals($expected, $result);
$result = $this->Number->currency('22.389', 'CAD');
$expected = '$22.39';
$this->assertEquals($expected, $result);
$result = $this->Number->currency('4.111', 'AUD');
$expected = '$4.11';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
@ -317,11 +350,11 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$this->Number->defaultCurrency('EUR'); $this->Number->defaultCurrency('EUR');
$result = $this->Number->currency(1000); $result = $this->Number->currency(1000);
$expected = '€1.000,00'; $expected = '1.000,00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency(2000); $result = $this->Number->currency(2000);
$expected = '€2.000,00'; $expected = '2.000,00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$this->Number->defaultCurrency('USD'); $this->Number->defaultCurrency('USD');
@ -352,11 +385,11 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'EUR'); $result = $this->Number->currency($value, 'EUR');
$expected = '€100.100.100,00'; $expected = '100.100.100,00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'GBP'); $result = $this->Number->currency($value, 'GBP');
$expected = '£100,100,100.00'; $expected = '£100,100,100.00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
@ -373,11 +406,11 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'EUR'); $result = $this->Number->currency($value, 'EUR');
$expected = '(€100.100.100,00)'; $expected = '(100.100.100,00)';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'GBP'); $result = $this->Number->currency($value, 'GBP');
$expected = '(£100,100,100.00)'; $expected = '(£100,100,100.00)';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'USD', array('negative' => '-')); $result = $this->Number->currency($value, 'USD', array('negative' => '-'));
@ -385,11 +418,11 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'EUR', array('negative' => '-')); $result = $this->Number->currency($value, 'EUR', array('negative' => '-'));
$expected = '-€100.100.100,00'; $expected = '-100.100.100,00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'GBP', array('negative' => '-')); $result = $this->Number->currency($value, 'GBP', array('negative' => '-'));
$expected = '-£100,100,100.00'; $expected = '-£100,100,100.00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
@ -406,7 +439,7 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'EUR'); $result = $this->Number->currency($value, 'EUR');
$expected = '€0,99'; $expected = '0,99';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'GBP'); $result = $this->Number->currency($value, 'GBP');
@ -427,7 +460,7 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'EUR'); $result = $this->Number->currency($value, 'EUR');
$expected = '(€0,99)'; $expected = '(0,99)';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'GBP'); $result = $this->Number->currency($value, 'GBP');
@ -439,7 +472,7 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'EUR', array('negative' => '-')); $result = $this->Number->currency($value, 'EUR', array('negative' => '-'));
$expected = '-€0,99'; $expected = '-0,99';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'GBP', array('negative' => '-')); $result = $this->Number->currency($value, 'GBP', array('negative' => '-'));
@ -460,11 +493,11 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'EUR'); $result = $this->Number->currency($value, 'EUR');
$expected = '€0,00'; $expected = '0,00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'GBP'); $result = $this->Number->currency($value, 'GBP');
$expected = '£0.00'; $expected = '£0.00';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'GBP', array('zero' => 'FREE!')); $result = $this->Number->currency($value, 'GBP', array('zero' => 'FREE!'));
@ -485,7 +518,7 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'GBP', array('places' => 0)); $result = $this->Number->currency($value, 'GBP', array('places' => 0));
$expected = '£1,234,568'; $expected = '£1,234,568';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency('1234567.8912345', null, array('before' => 'GBP', 'places' => 3)); $result = $this->Number->currency('1234567.8912345', null, array('before' => 'GBP', 'places' => 3));
@ -496,15 +529,15 @@ class CakeNumberTest extends CakeTestCase {
$expected = 'GBP650.1200'; $expected = 'GBP650.1200';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency($value, 'GBP', array('escape' => true)); $result = $this->Number->currency($value, 'GBP', array('before'=>'£ ', 'escape' => true));
$expected = '£1,234,567.89'; $expected = '£ 1,234,567.89';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency('0.35', 'USD', array('after' => false)); $result = $this->Number->currency('0.35', 'USD', array('after' => false));
$expected = '$0.35'; $expected = '$0.35';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency('0.35', 'GBP', array('after' => false)); $result = $this->Number->currency('0.35', 'GBP', array('before'=>'£', 'after' => false, 'escape' => false));
$expected = '£0.35'; $expected = '£0.35';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
@ -513,7 +546,7 @@ class CakeNumberTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$result = $this->Number->currency('0.35', 'EUR'); $result = $this->Number->currency('0.35', 'EUR');
$expected = '€0,35'; $expected = '0,35';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }

View file

@ -37,18 +37,30 @@ class CakeNumber {
* @var array * @var array
*/ */
protected static $_currencies = array( protected static $_currencies = array(
'AUD' => array(
'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true
),
'CAD' => array(
'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true
),
'USD' => array( 'USD' => array(
'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after', 'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true
), ),
'GBP' => array(
'wholeSymbol' => '£', 'wholePosition' => 'before', 'fractionSymbol' => 'p', 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()','escape' => false
),
'EUR' => array( 'EUR' => array(
'wholeSymbol' => '€', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after', 'wholeSymbol' => '€', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => false 'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => true
) ),
'GBP' => array(
'wholeSymbol' => '£', 'wholePosition' => 'before', 'fractionSymbol' => 'p', 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()','escape' => true
),
'JPY' => array(
'wholeSymbol' => '¥', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true
),
); );
/** /**
@ -189,7 +201,8 @@ class CakeNumber {
$escape = true; $escape = true;
if (is_array($options)) { if (is_array($options)) {
$options = array_merge(array('before' => '$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options); $defaults = array('before' => '$', 'places' => 2, 'thousands' => ',', 'decimals' => '.');
$options += $defaults;
extract($options); extract($options);
} }
@ -248,7 +261,7 @@ class CakeNumber {
$after = substr($value, $foundDecimal); $after = substr($value, $foundDecimal);
$value = substr($value, 0, $foundDecimal); $value = substr($value, 0, $foundDecimal);
} }
while (($foundThousand = preg_replace('/(\d+)(\d\d\d)/', '\1 \2', $value)) != $value) { while (($foundThousand = preg_replace('/(\d+)(\d\d\d)/', '\1 \2', $value)) !== $value) {
$value = $foundThousand; $value = $foundThousand;
} }
$value .= $after; $value .= $after;
@ -281,8 +294,8 @@ class CakeNumber {
* the number will be wrapped with ( and ) * the number will be wrapped with ( and )
* - `escape` - Should the output be escaped for html special characters. * - `escape` - Should the output be escaped for html special characters.
* The default value for this option is controlled by the currency settings. * The default value for this option is controlled by the currency settings.
* By default the EUR, and GBP contain HTML encoded symbols. If you require non HTML * By default all currencies contain utf-8 symbols and don't need this changed. If you require
* encoded symbols you will need to update the settings with the correct bytes. * non HTML encoded symbols you will need to update the settings with the correct bytes.
* *
* @param float $value * @param float $value
* @param string $currency Shortcut to default options. Valid values are * @param string $currency Shortcut to default options. Valid values are