Skip to content

Commit 19ad8a8

Browse files
committed
fixed yiisoft#17708 (intl number formatter)
1 parent 1378ca0 commit 19ad8a8

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

framework/i18n/Formatter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,9 +1219,14 @@ public function asScientific($value, $decimals = null, $options = [], $textOptio
12191219
if ($value === null) {
12201220
return $this->nullDisplay;
12211221
}
1222+
12221223
$value = $this->normalizeNumericValue($value);
12231224

12241225
if ($this->_intlLoaded) {
1226+
// fix for https://github.com/yiisoft/yii2/issues/17708
1227+
if ($decimals !== null) {
1228+
$decimals++;
1229+
}
12251230
$f = $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $decimals, $options, $textOptions);
12261231
if (($result = $f->format($value)) === false) {
12271232
throw new InvalidArgumentException('Formatting scientific number value failed: ' . $f->getErrorCode() . ' ' . $f->getErrorMessage());

tests/framework/i18n/FormatterNumberTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ public function testAsCurrency()
515515
public function testIntlAsScientific()
516516
{
517517
$value = '123';
518-
$this->assertSame('1.23E2', $this->formatter->asScientific($value));
518+
$this->assertSame('1.23E2', $this->formatter->asScientific($value, 2));
519519
$value = '123456';
520520
$this->assertSame('1.23456E5', $this->formatter->asScientific($value));
521521
$value = '-123456.123';
@@ -528,7 +528,10 @@ public function testIntlAsScientific()
528528
// null display
529529
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null));
530530

531-
$this->assertSame('8.76543210987654E16', $this->formatter->asScientific('87654321098765436'));
531+
// precision (see also https://github.com/yiisoft/yii2/issues/17708)
532+
$this->assertSame('9E16', $this->formatter->asScientific('87654321098765436', 0));
533+
$this->assertSame('8.8E16', $this->formatter->asScientific('87654321098765436', 1));
534+
$this->assertSame('8.765432109877E16', $this->formatter->asScientific('87654321098765436', 12));
532535
}
533536

534537
public function testAsScientific()
@@ -548,6 +551,11 @@ public function testAsScientific()
548551
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null));
549552

550553
$this->assertSame('8.765432E+16', $this->formatter->asScientific('87654321098765436'));
554+
555+
// precision (see also https://github.com/yiisoft/yii2/issues/17708)
556+
$this->assertSame('9E+16', $this->formatter->asScientific('87654321098765436', 0));
557+
$this->assertSame('8.8E+16', $this->formatter->asScientific('87654321098765436', 1));
558+
$this->assertSame('8.765432109877E+16', $this->formatter->asScientific('87654321098765436', 12));
551559
}
552560

553561
public function testIntlAsSpellout()

0 commit comments

Comments
 (0)