diff --git a/framework/i18n/Formatter.php b/framework/i18n/Formatter.php index 414e9717f0e..4e9260d3b35 100644 --- a/framework/i18n/Formatter.php +++ b/framework/i18n/Formatter.php @@ -1219,6 +1219,7 @@ public function asScientific($value, $decimals = null, $options = [], $textOptio if ($value === null) { return $this->nullDisplay; } + $value = $this->normalizeNumericValue($value); if ($this->_intlLoaded) { diff --git a/tests/framework/i18n/FormatterNumberTest.php b/tests/framework/i18n/FormatterNumberTest.php index 3609bd41c9e..055565d3f6b 100755 --- a/tests/framework/i18n/FormatterNumberTest.php +++ b/tests/framework/i18n/FormatterNumberTest.php @@ -515,7 +515,7 @@ public function testAsCurrency() public function testIntlAsScientific() { $value = '123'; - $this->assertSame('1.23E2', $this->formatter->asScientific($value)); + $this->assertSame('1.23E2', $this->formatter->asScientific($value, 2)); $value = '123456'; $this->assertSame('1.23456E5', $this->formatter->asScientific($value)); $value = '-123456.123'; @@ -528,7 +528,10 @@ public function testIntlAsScientific() // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null)); - $this->assertSame('8.76543210987654E16', $this->formatter->asScientific('87654321098765436')); + // precision (see also https://github.com/yiisoft/yii2/issues/17708) + $this->assertSame('9E16', $this->formatter->asScientific('87654321098765436', 0)); + $this->assertSame('8.8E16', $this->formatter->asScientific('87654321098765436', 1)); + $this->assertSame('8.765432109877E16', $this->formatter->asScientific('87654321098765436', 12)); } public function testAsScientific() @@ -548,6 +551,11 @@ public function testAsScientific() $this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null)); $this->assertSame('8.765432E+16', $this->formatter->asScientific('87654321098765436')); + + // precision (see also https://github.com/yiisoft/yii2/issues/17708) + $this->assertSame('9E+16', $this->formatter->asScientific('87654321098765436', 0)); + $this->assertSame('8.8E+16', $this->formatter->asScientific('87654321098765436', 1)); + $this->assertSame('8.765432109877E+16', $this->formatter->asScientific('87654321098765436', 12)); } public function testIntlAsSpellout()