Skip to content

Commit

Permalink
Fixing and adding tests for setting places and precision in Number::c…
Browse files Browse the repository at this point in the history
…urrency()
  • Loading branch information
lorenzo committed Aug 2, 2014
1 parent 617b98e commit d7946e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
14 changes: 11 additions & 3 deletions src/Utility/Number.php
Expand Up @@ -315,15 +315,23 @@ public static function currency($value, $currency = null, array $options = array
}

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

if (isset($options['places'])) {
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $options['places']);
}

if (isset($options['precision'])) {
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $options['precision']);
}

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

if (!empty($options['fractionSymbol']) && $value > 0 && $value < 1) {
$places = isset($options['places']) ? $options['places'] : 2;
$value = $value * pow(10, $places);
$value = $value * 100;
$pos = isset($options['fractionPosition']) ? $options['fractionPosition'] : 'after';
return static::format($value, ['places' => 0, $pos => $options['fractionSymbol']]);
return static::format($value, ['precision' => 0, $pos => $options['fractionSymbol']]);
}

$before = isset($options['before']) ? $options['before'] : null;
Expand Down
22 changes: 6 additions & 16 deletions tests/TestCase/Utility/NumberTest.php
Expand Up @@ -243,30 +243,20 @@ public function testCurrency() {
* @return void
*/
public function testCurrencyWithFractionAndPlaces() {
$result = $this->Number->currency('1.23', 'GBP', array('places' => 3));
$expected = '£1.230';
$result = $this->Number->currency('1.23', 'EUR', ['locale' => 'de_DE', 'places' => 3]);
$expected = '1,230 €';
$this->assertEquals($expected, $result);

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

$result = $this->Number->currency('0.001', 'GBP', array('places' => 3));
$result = $this->Number->currency('0.001', 'GBP', ['places' => 3, 'fractionSymbol' => 'p']);
$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';
$result = $this->Number->currency('1.23', 'EUR', ['locale' => 'de_DE', 'precision' => 1]);
$expected = '1,2 €';
$this->assertEquals($expected, $result);
}

Expand Down

0 comments on commit d7946e0

Please sign in to comment.