Fix #20475: Fix Formatter class asScientific() method for PHP 8.5 sprintf precision change (6 to 0)

This commit is contained in:
Wilmer Arambula
2025-08-12 00:51:01 -04:00
committed by GitHub
parent 7c96143d16
commit 36d67cd6a6
3 changed files with 11 additions and 4 deletions

View File

@ -12,7 +12,7 @@ Yii Framework 2 Change Log
- Bug #20453: Fix PHPStan/Psalm types in `yii\web\View` (max-s-lab)
- Enh #20461: Add PHPStan/Psalm annotations for `yii\filters\auth\AuthInterface` (max-s-lab)
- Bug #20459: Fix return type in `RequestParserInterface::parse` (max-s-lab)
- Bug #20475: Fix `Formatter` class `asScientific()` method for PHP `8.5` `sprintf` precision change (`6` to `0`) (terabytesoftw)
2.0.53 June 27, 2025
--------------------

View File

@ -1261,10 +1261,12 @@ class Formatter extends Component
if ($value === null) {
return $this->nullDisplay;
}
$value = $this->normalizeNumericValue($value);
if ($this->_intlLoaded) {
$f = $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $decimals, $options, $textOptions);
if (($result = $f->format($value)) === false) {
throw new InvalidArgumentException('Formatting scientific number value failed: ' . $f->getErrorCode() . ' ' . $f->getErrorMessage());
}
@ -1276,7 +1278,12 @@ class Formatter extends Component
return sprintf("%.{$decimals}E", $value);
}
return sprintf('%.E', $value);
// PHP 8.5+ changed sprintf('%.E') behavior: empty precision now defaults to '0' instead of '6'
// Specify explicit precision to maintain backward compatibility
// @link https://github.com/php/php-src/commit/5ed8b2be5533fbd4db95d9724d268eb9c9741f14
$format = PHP_VERSION_ID >= 80500 ? '%.6E' : '%.E';
return sprintf($format, $value);
}
/**