Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Top 6 world currencies and make them utf8 per default

  • Loading branch information...
commit eb874f87a34a5ad496745a5549dd91cf15b7a7bf 1 parent 9cffb0b
@dereuromark dereuromark authored
View
103 lib/Cake/Test/Case/Utility/CakeNumberTest.php
@@ -81,6 +81,12 @@ public function testFormat() {
$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 @@ public function testFormatDelta() {
$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 @@ public function testCurrency() {
$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 @@ public function testCurrency() {
$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 @@ public function testCurrency() {
$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 @@ public function testDefaultCurrency() {
$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 @@ public function testCurrencyPositive() {
$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 @@ public function testCurrencyNegative() {
$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 @@ public function testCurrencyNegative() {
$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 @@ public function testCurrencyCentsPositive() {
$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 @@ public function testCurrencyCentsNegative() {
$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 @@ public function testCurrencyCentsNegative() {
$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 @@ public function testCurrencyZero() {
$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 @@ public function testCurrencyOptions() {
$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 @@ public function testCurrencyOptions() {
$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));
@ADmad Collaborator
ADmad added a note

Missing space around => and also on line 540.

@dereuromark Collaborator

Thx. Corrected it right away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $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 @@ public function testCurrencyOptions() {
$this->assertEquals($expected, $result);
$result = $this->Number->currency('0.35', 'EUR');
- $expected = '€0,35';
+ $expected = '0,35';
$this->assertEquals($expected, $result);
}
View
33 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
),
+ 'EUR' => array(
+ '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' => false
+ '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
),
- 'EUR' => array(
- 'wholeSymbol' => '€', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
- 'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => false
- )
);
/**
@@ -189,7 +201,8 @@ public static function format($value, $options = false) {
$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 @@ protected static function _numberFormat($value, $places = 0, $decimals = '.', $t
$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 @@ protected static function _numberFormat($value, $places = 0, $decimals = '.', $t
* 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
Please sign in to comment.
Something went wrong with that request. Please try again.