Skip to content

Commit

Permalink
[Form] NumberType: Fix parsing of numbers in exponential notation wit…
Browse files Browse the repository at this point in the history
…h negative exponent
  • Loading branch information
jbtronics authored and nicolas-grekas committed Aug 7, 2024
1 parent ff45013 commit dc6e748
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public function reverseTransform($value)
$value = str_replace(',', $decSep, $value);
}

if (str_contains($value, $decSep)) {
//If the value is in exponential notation with a negative exponent, we end up with a float value too
if (str_contains($value, $decSep) || false !== stripos($value, 'e-')) {
$type = \NumberFormatter::TYPE_DOUBLE;
} else {
$type = \PHP_INT_SIZE === 8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,31 @@ public function testReverseTransformSmallInt()

$this->assertSame(1.0, $transformer->reverseTransform('1'));
}

/**
* @dataProvider eNotationProvider
*/
public function testReverseTransformENotation($output, $input)
{
\Locale::setDefault('en');

$transformer = new NumberToLocalizedStringTransformer();

$this->assertSame($output, $transformer->reverseTransform($input));
}

public static function eNotationProvider(): array
{
return [
[0.001, '1E-3'],
[0.001, '1.0E-3'],
[0.001, '1e-3'],
[0.001, '1.0e-03'],
[1000.0, '1E3'],
[1000.0, '1.0E3'],
[1000.0, '1e3'],
[1000.0, '1.0e3'],
[1232.0, '1.232e3'],
];
}
}

0 comments on commit dc6e748

Please sign in to comment.