Skip to content

Commit

Permalink
More improvents to Number::currency()
Browse files Browse the repository at this point in the history
Removed the feature to auto format negative numbers into parenthesis
Removed the ability to set a custom currency symbol as it can be done with pattern
  • Loading branch information
lorenzo committed Aug 2, 2014
1 parent ea2aa04 commit 617b98e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 92 deletions.
68 changes: 9 additions & 59 deletions src/Utility/Number.php
Expand Up @@ -65,17 +65,6 @@ class Number {
),
);

/**
* Default options for currency formats
*
* @var array
*/
protected static $_currencyDefaults = array(
'wholeSymbol' => '', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
'fractionExponent' => 2
);

/**
* A list of number formatters indexed by locale
*
Expand Down Expand Up @@ -306,6 +295,7 @@ public static function formatDelta($value, array $options = array()) {
*/
public static function currency($value, $currency = null, array $options = array()) {
$value = (float)$value;
$currency = $currency ?: static::defaultCurrency();

if (isset($options['zero']) && !$value) {
return $options['zero'];
Expand All @@ -324,61 +314,21 @@ public static function currency($value, $currency = null, array $options = array
);
}

if ($currency === null) {
$currency = static::defaultCurrency();
}

$formatter = static::$_currencyFormatters[$locale];

if (!empty($options['pattern'])) {
$formatter->setPattern($options['pattern']);
}

return $formatter->formatCurrency($value, $currency);

$default = static::$_currencyDefaults;


$options += $default;

if (isset($options['before']) && $options['before'] !== '') {
$options['wholeSymbol'] = $options['before'];
}
if (isset($options['after']) && !$options['after'] !== '') {
$options['fractionSymbol'] = $options['after'];
}

$result = $options['before'] = $options['after'] = null;

$symbolKey = 'whole';
;
if (!$value) {
if ($options['zero'] !== 0) {
return $options['zero'];
}
} elseif ($value < 1 && $value > -1) {
if ($options['fractionSymbol'] !== false) {
$multiply = pow(10, $options['fractionExponent']);
$value = $value * $multiply;
$options['places'] = null;
$symbolKey = 'fraction';
}
if (!empty($options['fractionSymbol']) && $value > 0 && $value < 1) {
$places = isset($options['places']) ? $options['places'] : 2;
$value = $value * pow(10, $places);
$pos = isset($options['fractionPosition']) ? $options['fractionPosition'] : 'after';
return static::format($value, ['places' => 0, $pos => $options['fractionSymbol']]);
}

$position = $options[$symbolKey . 'Position'] !== 'after' ? 'before' : 'after';
$options[$position] = $options[$symbolKey . 'Symbol'];

$abs = abs($value);
$result = static::format($abs, $options);

if ($value < 0) {
if ($options['negative'] === '()') {
$result = '(' . $result . ')';
} else {
$result = $options['negative'] . $result;
}
}
return $result;
$before = isset($options['before']) ? $options['before'] : null;
$after = isset($options['after']) ? $options['after'] : null;
return $before . $formatter->formatCurrency($value, $currency) . $after;
}

/**
Expand Down
41 changes: 8 additions & 33 deletions tests/TestCase/Utility/NumberTest.php
Expand Up @@ -192,46 +192,25 @@ public function testCurrency() {
$expected = '(€1 235,03)';
$this->assertEquals($expected, $result);

$result = $this->Number->currency(0.5, 'USD');
$result = $this->Number->currency(0.5, 'USD', ['locale' => 'en_US', 'fractionSymbol' => 'c']);
$expected = '50c';
$this->assertEquals($expected, $result);

$result = $this->Number->currency(0.5, null, array('after' => 'øre'));
$expected = '50øre';
$this->assertEquals($expected, $result);

$options = array('wholeSymbol' => '$', 'wholePosition' => 'after', 'fractionSymbol' => ' cents');
$result = $this->Number->currency(0.2, null, $options);
$options = ['fractionSymbol' => ' cents'];
$result = $this->Number->currency(0.2, 'USD', $options);
$expected = '20 cents';
$this->assertEquals($expected, $result);

$options = array('wholeSymbol' => '$', 'wholePosition' => 'after', 'fractionSymbol' => 'cents ',
'fractionPosition' => 'before');
$options = ['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'));
$expected = '311.00$';
$this->assertEquals($expected, $result);

$result = $this->Number->currency(0.2, 'EUR');
$expected = '€0,20';
$expected = '€0.20';
$this->assertEquals($expected, $result);

$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);

$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);

$options = array('fractionSymbol' => false, 'fractionPosition' => 'before', 'wholeSymbol' => '$');
$options = ['fractionSymbol' => false, 'fractionPosition' => 'before'];
$result = $this->Number->currency(0.5, null, $options);
$expected = '$0.50';
$this->assertEquals($expected, $result);
Expand All @@ -248,16 +227,12 @@ public function testCurrency() {
$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';
$expected = 'CA$22.39';
$this->assertEquals($expected, $result);

$result = $this->Number->currency('4.111', 'AUD');
$expected = '$4.11';
$expected = 'A$4.11';
$this->assertEquals($expected, $result);
}

Expand Down

0 comments on commit 617b98e

Please sign in to comment.