Skip to content

Commit e6337fd

Browse files
bug symfony#57861 [Form] NumberType: Fix parsing of numbers in exponential notation with negative exponent (jbtronics)
This PR was submitted for the 7.1 branch but it was squashed and merged into the 5.4 branch instead. Discussion ---------- [Form] NumberType: Fix parsing of numbers in exponential notation with negative exponent | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | See below | License | MIT Currently, when inputting a string like "1E-3" into a NumberType leads incorrectly to a result of "0". The correct result would be "0.001", which is also the result, when inputting "1.0E-3". This is caused by `NumberToLocalizedStringTransformer`, which assumes that the result must be a integer, if the string does not contain the decimal separator. However, this behavior is incorrect, if the string is in exponential notation with negative exponent. The NumberFormatter can correctly parse this format, but it needs to be told that the result should be a double (otherwise its gets incorrectly rounded to 0) To fix this behavior, I added a check for the negative exponential format, which then results in a double result. Commits ------- 62c70bd [Form] NumberType: Fix parsing of numbers in exponential notation with negative exponent
2 parents fababe1 + 62c70bd commit e6337fd

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ public function reverseTransform($value)
143143
$value = str_replace(',', $decSep, $value);
144144
}
145145

146-
if (str_contains($value, $decSep)) {
146+
//If the value is in exponential notation with a negative exponent, we end up with a float value too
147+
if (str_contains($value, $decSep) || false !== stripos($value, 'e-')) {
147148
$type = \NumberFormatter::TYPE_DOUBLE;
148149
} else {
149150
$type = \PHP_INT_SIZE === 8

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,4 +632,31 @@ public function testReverseTransformSmallInt()
632632

633633
$this->assertSame(1.0, $transformer->reverseTransform('1'));
634634
}
635+
636+
/**
637+
* @dataProvider eNotationProvider
638+
*/
639+
public function testReverseTransformENotation($output, $input)
640+
{
641+
\Locale::setDefault('en');
642+
643+
$transformer = new NumberToLocalizedStringTransformer();
644+
645+
$this->assertSame($output, $transformer->reverseTransform($input));
646+
}
647+
648+
public static function eNotationProvider(): array
649+
{
650+
return [
651+
[0.001, '1E-3'],
652+
[0.001, '1.0E-3'],
653+
[0.001, '1e-3'],
654+
[0.001, '1.0e-03'],
655+
[1000.0, '1E3'],
656+
[1000.0, '1.0E3'],
657+
[1000.0, '1e3'],
658+
[1000.0, '1.0e3'],
659+
[1232.0, '1.232e3'],
660+
];
661+
}
635662
}

0 commit comments

Comments
 (0)