Skip to content

Commit 59c272b

Browse files
committed
bug symfony#24036 [Form] Fix precision of MoneyToLocalizedStringTransformer's divisions and multiplications (Rubinum)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] Fix precision of MoneyToLocalizedStringTransformer's divisions and multiplications | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | no | License | MIT There is a [PHP Bug](https://bugs.php.net/bug.php?id=75004) with the accuracy of divisions and multiplications when `/=` and `*=` are used. Here is the proof: https://3v4l.org/u1DkX It would be better to use `bcmul()` and `bcdiv()` in the `MoneyToLocalizedStringTransformer.php` to prevent this bug. Commits ------- ab47c78 Added improvement for accuracy in MoneyToLocalizedStringTransformer.
2 parents 5bbec9c + ab47c78 commit 59c272b

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ public function __construct($scale = 2, $grouping = true, $roundingMode = self::
5454
*/
5555
public function transform($value)
5656
{
57-
if (null !== $value) {
57+
if (null !== $value && 1 !== $this->divisor) {
5858
if (!is_numeric($value)) {
5959
throw new TransformationFailedException('Expected a numeric.');
6060
}
61-
62-
$value /= $this->divisor;
61+
$value = (string) ($value / $this->divisor);
6362
}
6463

6564
return parent::transform($value);
@@ -78,9 +77,8 @@ public function transform($value)
7877
public function reverseTransform($value)
7978
{
8079
$value = parent::reverseTransform($value);
81-
82-
if (null !== $value) {
83-
$value *= $this->divisor;
80+
if (null !== $value && 1 !== $this->divisor) {
81+
$value = (float) (string) ($value * $this->divisor);
8482
}
8583

8684
return $value;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,12 @@ public function testReverseTransformEmpty()
7272

7373
$this->assertNull($transformer->reverseTransform(''));
7474
}
75+
76+
public function testFloatToIntConversionMismatchOnReversTransform()
77+
{
78+
$transformer = new MoneyToLocalizedStringTransformer(null, null, null, 100);
79+
IntlTestHelper::requireFullIntl($this, false);
80+
\Locale::setDefault('de_AT');
81+
$this->assertSame(3655, (int) $transformer->reverseTransform('36,55'));
82+
}
7583
}

0 commit comments

Comments
 (0)