Skip to content

Commit

Permalink
Fix currency output for fraction values using fractionExponent option.
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Aug 6, 2013
1 parent 802228f commit ad6fcd4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
34 changes: 34 additions & 0 deletions lib/Cake/Test/Case/Utility/CakeNumberTest.php
Expand Up @@ -301,6 +301,40 @@ public function testCurrency() {
$this->assertEquals($expected, $result);
}

/**
* Test currency format with places and fraction exponents.
* Places should only matter for non fraction values and vice versa.
*
* @return void
*/
public function testCurrencyWithFractionAndPlaces() {
$result = $this->Number->currency('1.23', 'GBP', array('places' => 3));
$expected = '£1.230';
$this->assertEquals($expected, $result);

$result = $this->Number->currency('0.23', 'GBP', array('places' => 3));
$expected = '23p';
$this->assertEquals($expected, $result);

$result = $this->Number->currency('0.001', 'GBP', array('places' => 3));
$expected = '0p';
$this->assertEquals($expected, $result);

$this->Number->addFormat('BHD', array('before' => 'BD ', 'fractionSymbol' => ' fils',
'fractionExponent' => 3));
$result = $this->Number->currency('1.234', 'BHD', array('places' => 2));
$expected = 'BD 1.23';
$this->assertEquals($expected, $result);

$result = $this->Number->currency('0.234', 'BHD', array('places' => 2));
$expected = '234 fils';
$this->assertEquals($expected, $result);

$result = $this->Number->currency('0.001', 'BHD', array('places' => 2));
$expected = '1 fils';
$this->assertEquals($expected, $result);
}

/**
* Test adding currency format options to the number helper
*
Expand Down
25 changes: 17 additions & 8 deletions lib/Cake/Utility/CakeNumber.php
Expand Up @@ -39,27 +39,33 @@ class CakeNumber {
protected static $_currencies = array(
'AUD' => array(
'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,
'fractionExponent' => 2
),
'CAD' => array(
'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,
'fractionExponent' => 2
),
'USD' => array(
'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,
'fractionExponent' => 2
),
'EUR' => array(
'wholeSymbol' => '€', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => true
'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => true,
'fractionExponent' => 0
),
'GBP' => array(
'wholeSymbol' => '£', 'wholePosition' => 'before', 'fractionSymbol' => 'p', 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()','escape' => true
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()','escape' => true,
'fractionExponent' => 2
),
'JPY' => array(
'wholeSymbol' => '¥', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true
'wholeSymbol' => '¥', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
'fractionExponent' => 0
),
);

Expand All @@ -71,6 +77,7 @@ class CakeNumber {
protected static $_currencyDefaults = array(
'wholeSymbol' => '', 'wholePosition' => 'before', 'fractionSymbol' => '', 'fractionPosition' => 'after',
'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.','negative' => '()', 'escape' => true,
'fractionExponent' => 2
);

/**
Expand Down Expand Up @@ -230,6 +237,7 @@ public static function format($value, $options = false) {
* ### Options
*
* - `places` - Number of decimal places to use. ie. 2
* - `fractionExponent` - Fraction exponent of this specific currency. Defaults to 2.
* - `before` - The string to place before whole numbers. ie. '['
* - `after` - The string to place after decimal numbers. ie. ']'
* - `thousands` - Thousands separator ie. ','
Expand Down Expand Up @@ -297,6 +305,7 @@ protected static function _numberFormat($value, $places = 0, $decimals = '.', $t
* - `zero` - The text to use for zero values, can be a
* string or a number. ie. 0, 'Free!'
* - `places` - Number of decimal places to use. ie. 2
* - `fractionExponent` - Fraction exponent of this specific currency. Defaults to 2.
* - `thousands` - Thousands separator ie. ','
* - `decimals` - Decimal separator symbol ie. '.'
* - `negative` - Symbol for negative numbers. If equal to '()',
Expand Down Expand Up @@ -344,7 +353,7 @@ public static function currency($value, $currency = null, $options = array()) {
}
} elseif ($value < 1 && $value > -1) {
if ($options['fractionSymbol'] !== false) {
$multiply = intval('1' . str_pad('', $options['places'], '0'));
$multiply = pow(10, $options['fractionExponent']);
$value = $value * $multiply;
$options['places'] = null;
$symbolKey = 'fraction';
Expand Down

0 comments on commit ad6fcd4

Please sign in to comment.