Skip to content

Commit 1728935

Browse files
Merge branch '7.2' into 7.3
* 7.2: Fix php.net links fix version number in deprecation [DoctrineBridge] Restore compatibility with Doctrine ODM by validating $class object type chore: Increase minimum version of type-info component [FrameworkBundle] Add functional tests for the `ContainerLintCommand` command Fix precision loss when rounding large integers in `NumberToLocalizedStringTransformer`
2 parents e06b02d + 548fc99 commit 1728935

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ protected function castParsedValue(int|float $value): int|float
185185
*/
186186
private function round(int|float $number): int|float
187187
{
188+
if (\is_int($number)) {
189+
return $number;
190+
}
191+
188192
if (null !== $this->scale) {
189193
// shift number to maintain the correct scale during rounding
190194
$roundingCoef = 10 ** $this->scale;

Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,4 +726,33 @@ public static function eNotationProvider(): array
726726
[1232.0, '1.232e3'],
727727
];
728728
}
729+
730+
public function testReverseTransformDoesNotCauseIntegerPrecisionLoss()
731+
{
732+
$transformer = new NumberToLocalizedStringTransformer();
733+
734+
// Test a large integer that causes actual precision loss when cast to float
735+
$largeInt = \PHP_INT_MAX - 1; // This value loses precision when cast to float
736+
$result = $transformer->reverseTransform((string) $largeInt);
737+
738+
$this->assertSame($largeInt, $result);
739+
$this->assertIsInt($result);
740+
}
741+
742+
public function testRoundMethodKeepsIntegersAsIntegers()
743+
{
744+
$transformer = new NumberToLocalizedStringTransformer(2); // scale=2 triggers rounding
745+
746+
// Use reflection to test the private round() method directly
747+
$reflection = new \ReflectionClass($transformer);
748+
$roundMethod = $reflection->getMethod('round');
749+
$roundMethod->setAccessible(true);
750+
751+
$int = \PHP_INT_MAX - 1;
752+
$result = $roundMethod->invoke($transformer, $int);
753+
754+
// With the fix, integers should stay as integers, not be converted to floats
755+
$this->assertSame($int, $result);
756+
$this->assertIsInt($result);
757+
}
729758
}

0 commit comments

Comments
 (0)