diff --git a/ext/standard/JitNumberFormat.php b/ext/standard/JitNumberFormat.php index d153fded28b..30c2315e15b 100644 --- a/ext/standard/JitNumberFormat.php +++ b/ext/standard/JitNumberFormat.php @@ -8,8 +8,8 @@ use PHPCompiler\JIT\BasicBlockHelper; use PHPCompiler\JIT\Builtin\TypeErrorRaise; use PHPCompiler\JIT\Context; -use PHPCompiler\JIT\JitStringBuiltinArg; use PHPCompiler\JIT\JitRoundModeArg; +use PHPCompiler\JIT\JitStringBuiltinArg; use PHPCompiler\JIT\JitValueBox; use PHPCompiler\JIT\NamedOptionalCallArgs; use PHPCompiler\JIT\Variable as JITVariable; @@ -18,9 +18,9 @@ use PHPLLVM\Value; /** - * LLVM JIT/AOT helper for number_format() (int/float/numeric string, 0–4 args; subset of PHP). + * LLVM JIT/AOT helper for number_format() (int/float/numeric string, 0–5 args; subset of PHP). * - * php-src: ext/standard/number_format.c — Z_PARAM_LONG / Z_PARAM_STR + * php-src: ext/standard/number_format.c — Z_PARAM_LONG / Z_PARAM_STR / RoundingMode */ final class JitNumberFormat { @@ -29,9 +29,10 @@ public static function format(Context $context, JITVariable ...$args): Value $argc = count($args); $maxArgs = CompilerVersion::supportsRoundingModeEnum() ? 5 : 4; if ($argc < 1 || $argc > $maxArgs) { - throw new \LogicException( - sprintf('number_format() requires one to %d arguments', $maxArgs) - ); + throw new \LogicException(\sprintf( + 'number_format() requires one to %d arguments', + $maxArgs + )); } if ($context->callerStrictTypes) { diff --git a/ext/standard/VmNumberFormat.php b/ext/standard/VmNumberFormat.php index ddb9730a4c9..ee828f8ec40 100644 --- a/ext/standard/VmNumberFormat.php +++ b/ext/standard/VmNumberFormat.php @@ -81,11 +81,7 @@ public static function format( $pow *= 10; } - if ($decimals > 0) { - $rounded = VmRound::mathRound($number, $decimals, $roundingMode); - } else { - $rounded = VmRound::mathRound($number, 0, $roundingMode); - } + $rounded = VmRound::mathRound($number, $decimals, $roundingMode); $intPart = (int) floor($rounded); $fracPart = 0; diff --git a/ext/standard/number_format.php b/ext/standard/number_format.php index 2eb4a520375..2128da6cdea 100644 --- a/ext/standard/number_format.php +++ b/ext/standard/number_format.php @@ -23,7 +23,7 @@ /** * number_format() for integers and floats (C-style locale subset; LLVM JIT/AOT). * - * php-src: ext/standard/number_format.c — Z_PARAM_LONG / Z_PARAM_STR + * php-src: ext/standard/number_format.c — Z_PARAM_LONG / Z_PARAM_STR / RoundingMode */ final class number_format extends Internal { @@ -74,7 +74,7 @@ public function execute(Frame $frame): void 'number_format', 3, 'thousands_separator' - ) ?? ',' + ) ?? ',' : ','; $roundingMode = StdlibConstants::PHP_ROUND_HALF_UP; if (CompilerVersion::supportsRoundingModeEnum() && isset($frame->calledArgs[4])) { diff --git a/test/compliance/cases/stdlib/number_format_rounding_mode.phpt b/test/compliance/cases/stdlib/number_format_rounding_mode.phpt index 47883ec7710..1452da1ce6f 100644 --- a/test/compliance/cases/stdlib/number_format_rounding_mode.phpt +++ b/test/compliance/cases/stdlib/number_format_rounding_mode.phpt @@ -1,15 +1,24 @@ --TEST-- -number_format() optional RoundingMode (PHP 8.4, #9438) ---EXTENSIONS-- +stdlib number_format() optional rounding_mode (PHP 8.4, ext/standard/number_format.c, #9438) --FILE-- + +var_export(number_format(2.5, 0, '.', '', RoundingMode::HalfAwayFromZero)); +echo "\n"; +var_export(number_format(2.5, 0, '.', '', RoundingMode::TowardsZero)); +echo "\n"; +var_export(number_format(1.55, 1, '.', '', RoundingMode::HalfAwayFromZero)); +echo "\n"; +var_export(number_format(2.5, 0, '.', '', RoundingMode::HalfEven)); +echo "\n"; --EXPECT-- -1.6 -2 +'3' +'2' +'1.6' +'2' diff --git a/test/compliance/cases/stdlib/number_format_rounding_mode_jit.phpt b/test/compliance/cases/stdlib/number_format_rounding_mode_jit.phpt new file mode 100644 index 00000000000..0fcd11b3466 --- /dev/null +++ b/test/compliance/cases/stdlib/number_format_rounding_mode_jit.phpt @@ -0,0 +1,11 @@ +--TEST-- +stdlib number_format() optional rounding_mode JIT (#9438, ext/standard/number_format.c) +--FILE-- +assertSame('nan', SprintfJitHelper::numberFormat(NAN, 0, '.', ',')); + $this->assertSame( + '3', + SprintfJitHelper::numberFormat(2.5, 0, '.', '', StdlibConstants::PHP_ROUND_HALF_UP) + ); + $this->assertSame( + '2', + SprintfJitHelper::numberFormat( + 2.5, + 0, + '.', + '', + StdlibConstants::PHP_ROUND_TOWARD_ZERO + ) + ); } }