diff --git a/lib/Cake/Test/Case/Utility/CakeNumberTest.php b/lib/Cake/Test/Case/Utility/CakeNumberTest.php index b5fa5e832..48208c7bd 100644 --- a/lib/Cake/Test/Case/Utility/CakeNumberTest.php +++ b/lib/Cake/Test/Case/Utility/CakeNumberTest.php @@ -81,6 +81,12 @@ class CakeNumberTest extends CakeTestCase { $result = $this->Number->format($value, array('places' => 1)); $expected = '$0.0'; $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]'; $this->assertEquals($expected, $result); + $result = $this->Number->formatDelta(-$value, array('before' => '[ ', 'after' => ' ]')); + $expected = '[ -100,100,100.00 ]'; + $this->assertEquals($expected, $result); + $value = 0; $result = $this->Number->formatDelta($value, array('places' => 1, 'before' => '[', 'after' => ']')); $expected = '[0.0]'; @@ -196,19 +206,22 @@ class CakeNumberTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'EUR'); - $expected = '€100.100.100,00'; + $expected = '€100.100.100,00'; $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'GBP'); - $expected = '£100,100,100.00'; + $expected = '£100,100,100.00'; $this->assertEquals($expected, $result); - $result = $this->Number->currency($value, '', array('thousands' => ' ', 'wholeSymbol' => '€', 'wholePosition' => 'after', 'decimals' => ',', 'zero' => 'Gratuit')); - $expected = '100 100 100,00€'; + $options = array('thousands' => ' ', 'wholeSymbol' => 'EUR ', 'wholePosition' => 'before', + 'decimals' => ',', 'zero' => 'Gratuit'); + $result = $this->Number->currency($value, '', $options); + $expected = 'EUR 100 100 100,00'; $this->assertEquals($expected, $result); - $result = $this->Number->currency(1000.45, null, array('after' => 'øre', 'before' => 'Kr. ', 'decimals' => ',', 'thousands' => '.')); - $expected = 'Kr. 1.000,45'; + $options = array('after' => 'øre', 'before' => 'Kr.', 'decimals' => ',', 'thousands' => '.'); + $result = $this->Number->currency(1000.45, null, $options); + $expected = 'Kr.1.000,45'; $this->assertEquals($expected, $result); $result = $this->Number->currency(0.5, 'USD'); @@ -227,12 +240,15 @@ class CakeNumberTest extends CakeTestCase { $expected = '1.00 $'; $this->assertEquals($expected, $result); - $result = $this->Number->currency(0.2, null, array('wholeSymbol' => ' $', 'wholePosition' => 'after', 'fractionSymbol' => 'cents')); - $expected = '20cents'; + $options = array('wholeSymbol' => '$', 'wholePosition' => 'after', 'fractionSymbol' => ' cents'); + $result = $this->Number->currency(0.2, null, $options); + $expected = '20 cents'; $this->assertEquals($expected, $result); - $result = $this->Number->currency(0.2, null, array('wholeSymbol' => ' $', 'wholePosition' => 'after', 'fractionSymbol' => 'cents', 'fractionPosition' => 'before')); - $expected = 'cents20'; + $options = array('wholeSymbol' => '$', 'wholePosition' => 'after', 'fractionSymbol' => 'cents ', + 'fractionPosition' => 'before'); + $result = $this->Number->currency(0.2, null, $options); + $expected = 'cents 20'; $this->assertEquals($expected, $result); $result = $this->Number->currency(311, 'USD', array('wholePosition' => 'after')); @@ -240,31 +256,48 @@ class CakeNumberTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Number->currency(0.2, 'EUR'); - $expected = '€0,20'; + $expected = '€0,20'; $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'; $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'; $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'; $this->assertEquals($expected, $result); $result = $this->Number->currency(0, 'GBP'); - $expected = '£0.00'; + $expected = '£0.00'; $this->assertEquals($expected, $result); $result = $this->Number->currency(0.00000, 'GBP'); - $expected = '£0.00'; + $expected = '£0.00'; $this->assertEquals($expected, $result); $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); } @@ -317,11 +350,11 @@ class CakeNumberTest extends CakeTestCase { $this->assertEquals($expected, $result); $this->Number->defaultCurrency('EUR'); $result = $this->Number->currency(1000); - $expected = '€1.000,00'; + $expected = '€1.000,00'; $this->assertEquals($expected, $result); $result = $this->Number->currency(2000); - $expected = '€2.000,00'; + $expected = '€2.000,00'; $this->assertEquals($expected, $result); $this->Number->defaultCurrency('USD'); @@ -352,11 +385,11 @@ class CakeNumberTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'EUR'); - $expected = '€100.100.100,00'; + $expected = '€100.100.100,00'; $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'GBP'); - $expected = '£100,100,100.00'; + $expected = '£100,100,100.00'; $this->assertEquals($expected, $result); } @@ -373,11 +406,11 @@ class CakeNumberTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'EUR'); - $expected = '(€100.100.100,00)'; + $expected = '(€100.100.100,00)'; $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'GBP'); - $expected = '(£100,100,100.00)'; + $expected = '(£100,100,100.00)'; $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'USD', array('negative' => '-')); @@ -385,11 +418,11 @@ class CakeNumberTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'EUR', array('negative' => '-')); - $expected = '-€100.100.100,00'; + $expected = '-€100.100.100,00'; $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'GBP', array('negative' => '-')); - $expected = '-£100,100,100.00'; + $expected = '-£100,100,100.00'; $this->assertEquals($expected, $result); } @@ -406,7 +439,7 @@ class CakeNumberTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'EUR'); - $expected = '€0,99'; + $expected = '€0,99'; $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'GBP'); @@ -427,7 +460,7 @@ class CakeNumberTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'EUR'); - $expected = '(€0,99)'; + $expected = '(€0,99)'; $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'GBP'); @@ -439,7 +472,7 @@ class CakeNumberTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'EUR', array('negative' => '-')); - $expected = '-€0,99'; + $expected = '-€0,99'; $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'GBP', array('negative' => '-')); @@ -460,11 +493,11 @@ class CakeNumberTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'EUR'); - $expected = '€0,00'; + $expected = '€0,00'; $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'GBP'); - $expected = '£0.00'; + $expected = '£0.00'; $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'GBP', array('zero' => 'FREE!')); @@ -485,7 +518,7 @@ class CakeNumberTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Number->currency($value, 'GBP', array('places' => 0)); - $expected = '£1,234,568'; + $expected = '£1,234,568'; $this->assertEquals($expected, $result); $result = $this->Number->currency('1234567.8912345', null, array('before' => 'GBP', 'places' => 3)); @@ -496,15 +529,15 @@ class CakeNumberTest extends CakeTestCase { $expected = 'GBP650.1200'; $this->assertEquals($expected, $result); - $result = $this->Number->currency($value, 'GBP', array('escape' => true)); - $expected = '£1,234,567.89'; + $result = $this->Number->currency($value, 'GBP', array('before'=>'£ ', 'escape' => true)); + $expected = '£ 1,234,567.89'; $this->assertEquals($expected, $result); $result = $this->Number->currency('0.35', 'USD', array('after' => false)); $expected = '$0.35'; $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'; $this->assertEquals($expected, $result); @@ -513,7 +546,7 @@ class CakeNumberTest extends CakeTestCase { $this->assertEquals($expected, $result); $result = $this->Number->currency('0.35', 'EUR'); - $expected = '€0,35'; + $expected = '€0,35'; $this->assertEquals($expected, $result); } diff --git a/lib/Cake/Utility/CakeNumber.php b/lib/Cake/Utility/CakeNumber.php index 7f0b78b13..bab3a58d1 100644 --- a/lib/Cake/Utility/CakeNumber.php +++ b/lib/Cake/Utility/CakeNumber.php @@ -37,18 +37,30 @@ class CakeNumber { * @var 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( 'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after', '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( - 'wholeSymbol' => '€', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after', - 'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => false - ) + 'wholeSymbol' => '€', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after', + '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; 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); } @@ -248,7 +261,7 @@ class CakeNumber { $after = substr($value, $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 .= $after; @@ -281,8 +294,8 @@ class CakeNumber { * the number will be wrapped with ( and ) * - `escape` - Should the output be escaped for html special characters. * 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 - * encoded symbols you will need to update the settings with the correct bytes. + * By default all currencies contain utf-8 symbols and don't need this changed. If you require + * non HTML encoded symbols you will need to update the settings with the correct bytes. * * @param float $value * @param string $currency Shortcut to default options. Valid values are