From d7946e041ee0de755b91e1473ccdf05c085acc0a Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sat, 2 Aug 2014 23:17:37 +0200 Subject: [PATCH] Fixing and adding tests for setting places and precision in Number::currency() --- src/Utility/Number.php | 14 +++++++++++--- tests/TestCase/Utility/NumberTest.php | 22 ++++++---------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/Utility/Number.php b/src/Utility/Number.php index fd23a21d231..7e8d323e586 100644 --- a/src/Utility/Number.php +++ b/src/Utility/Number.php @@ -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; diff --git a/tests/TestCase/Utility/NumberTest.php b/tests/TestCase/Utility/NumberTest.php index 6e92108a2c7..73aadd31ae8 100644 --- a/tests/TestCase/Utility/NumberTest.php +++ b/tests/TestCase/Utility/NumberTest.php @@ -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); }