Skip to content

Commit d7946e0

Browse files
committed
Fixing and adding tests for setting places and precision in Number::currency()
1 parent 617b98e commit d7946e0

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/Utility/Number.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,23 @@ public static function currency($value, $currency = null, array $options = array
315315
}
316316

317317
$formatter = static::$_currencyFormatters[$locale];
318+
319+
if (isset($options['places'])) {
320+
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $options['places']);
321+
}
322+
323+
if (isset($options['precision'])) {
324+
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $options['precision']);
325+
}
326+
318327
if (!empty($options['pattern'])) {
319328
$formatter->setPattern($options['pattern']);
320329
}
321330

322331
if (!empty($options['fractionSymbol']) && $value > 0 && $value < 1) {
323-
$places = isset($options['places']) ? $options['places'] : 2;
324-
$value = $value * pow(10, $places);
332+
$value = $value * 100;
325333
$pos = isset($options['fractionPosition']) ? $options['fractionPosition'] : 'after';
326-
return static::format($value, ['places' => 0, $pos => $options['fractionSymbol']]);
334+
return static::format($value, ['precision' => 0, $pos => $options['fractionSymbol']]);
327335
}
328336

329337
$before = isset($options['before']) ? $options['before'] : null;

tests/TestCase/Utility/NumberTest.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,30 +243,20 @@ public function testCurrency() {
243243
* @return void
244244
*/
245245
public function testCurrencyWithFractionAndPlaces() {
246-
$result = $this->Number->currency('1.23', 'GBP', array('places' => 3));
247-
$expected = '£1.230';
246+
$result = $this->Number->currency('1.23', 'EUR', ['locale' => 'de_DE', 'places' => 3]);
247+
$expected = '1,230 €';
248248
$this->assertEquals($expected, $result);
249249

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

254-
$result = $this->Number->currency('0.001', 'GBP', array('places' => 3));
254+
$result = $this->Number->currency('0.001', 'GBP', ['places' => 3, 'fractionSymbol' => 'p']);
255255
$expected = '0p';
256256
$this->assertEquals($expected, $result);
257257

258-
$this->Number->addFormat('BHD', array('before' => 'BD ', 'fractionSymbol' => ' fils',
259-
'fractionExponent' => 3));
260-
$result = $this->Number->currency('1.234', 'BHD', array('places' => 2));
261-
$expected = 'BD 1.23';
262-
$this->assertEquals($expected, $result);
263-
264-
$result = $this->Number->currency('0.234', 'BHD', array('places' => 2));
265-
$expected = '234 fils';
266-
$this->assertEquals($expected, $result);
267-
268-
$result = $this->Number->currency('0.001', 'BHD', array('places' => 2));
269-
$expected = '1 fils';
258+
$result = $this->Number->currency('1.23', 'EUR', ['locale' => 'de_DE', 'precision' => 1]);
259+
$expected = '1,2 €';
270260
$this->assertEquals($expected, $result);
271261
}
272262

0 commit comments

Comments
 (0)