Browse Source

Fixes #17268: Fixed Formatter didn't take power into account

tags/2.0.18
Alexander Makarov 6 years ago committed by GitHub
parent
commit
33405fa4a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/CHANGELOG.md
  2. 27
      framework/i18n/Formatter.php
  3. 12
      tests/framework/i18n/FormatterNumberTest.php

1
framework/CHANGELOG.md

@ -6,6 +6,7 @@ Yii Framework 2 Change Log
- Bug #17220: Fixed error when using non-InputWidget in active form field (s1lver)
- Bug #17235: `yii\helpers\FileHelper::normalizePath()` now accepts stream wrappers (razvanphp)
- Bug #17268: Fixed Formatter didn't take power into account (samdark)
2.0.17 March 22, 2019

27
framework/i18n/Formatter.php

@ -1806,13 +1806,22 @@ class Formatter extends Component
*/
protected function normalizeNumericStringValue($value)
{
$separatorPosition = strrpos($value, '.');
$powerPosition = strrpos($value, 'E');
if ($powerPosition !== false) {
$valuePart = substr($value, 0, $powerPosition);
$powerPart = substr($value, $powerPosition + 1);
} else {
$powerPart = null;
$valuePart = $value;
}
$separatorPosition = strrpos($valuePart, '.');
if ($separatorPosition !== false) {
$integerPart = substr($value, 0, $separatorPosition);
$fractionalPart = substr($value, $separatorPosition + 1);
$integerPart = substr($valuePart, 0, $separatorPosition);
$fractionalPart = substr($valuePart, $separatorPosition + 1);
} else {
$integerPart = $value;
$integerPart = $valuePart;
$fractionalPart = null;
}
@ -1824,15 +1833,23 @@ class Formatter extends Component
if ($fractionalPart !== null) {
// truncate insignificant zeros
$fractionalPart = rtrim($fractionalPart, '0');
if (empty($fractionalPart)) {
$fractionalPart = $powerPart !== null ? '0' : null;
}
}
$normalizedValue = $integerPart;
if (!empty($fractionalPart)) {
if ($fractionalPart !== null) {
$normalizedValue .= '.' . $fractionalPart;
} elseif ($normalizedValue === '-0') {
$normalizedValue = '0';
}
if ($powerPart !== null) {
$normalizedValue .= 'E' . $powerPart;
}
return $normalizedValue;
}

12
tests/framework/i18n/FormatterNumberTest.php

@ -164,6 +164,10 @@ class FormatterNumberTest extends TestCase
$this->assertSame('123.1', $this->formatter->asDecimal($value, 1));
$this->assertSame('123', $this->formatter->asDecimal($value, 0));
// power values
$this->assertSame('2,000', $this->formatter->asDecimal(2e3));
$this->assertSame('0', $this->formatter->asDecimal(1E-10));
$value = 123;
$this->assertSame('123', $this->formatter->asDecimal($value));
$this->assertSame('123.00', $this->formatter->asDecimal($value, 2));
@ -226,7 +230,9 @@ class FormatterNumberTest extends TestCase
$this->assertSame('0.00', $this->formatter->asDecimal('0.00'));
$this->assertSame('0.01', $this->formatter->asDecimal('00000000000000.0100000000000'));
// power values
$this->assertSame('2,000.00', $this->formatter->asDecimal(2e3));
$this->assertSame('0.00', $this->formatter->asDecimal(1E-10));
$this->formatter->decimalSeparator = ',';
$this->formatter->thousandSeparator = '.';
@ -311,6 +317,9 @@ class FormatterNumberTest extends TestCase
$this->assertSame('$123,456.00', $this->formatter->asCurrency('123456'));
$this->assertSame('$0.00', $this->formatter->asCurrency('0'));
// power values
$this->assertSame('$0.00', $this->formatter->asCurrency(1E-10));
$this->formatter->locale = 'en-US';
$this->formatter->currencyCode = 'USD';
$this->assertSame('$123.00', $this->formatter->asCurrency('123'));
@ -474,6 +483,9 @@ class FormatterNumberTest extends TestCase
$this->assertSame('USD -123.45', $this->formatter->asCurrency('-123.45'));
$this->assertSame('USD -123.45', $this->formatter->asCurrency(-123.45));
// power values
$this->assertSame('USD 0.00', $this->formatter->asCurrency(1E-10));
$this->formatter->currencyCode = 'EUR';
$this->assertSame('EUR 123.00', $this->formatter->asCurrency('123'));
$this->assertSame('EUR 0.00', $this->formatter->asCurrency('0'));

Loading…
Cancel
Save