Skip to content

[Form] Fix precision of MoneyToLocalizedStringTransformer's divisions on transform() #26781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public function testFloatToIntConversionMismatchOnReversTransform()
$transformer = new MoneyToLocalizedStringTransformer(null, null, null, 100);
IntlTestHelper::requireFullIntl($this, false);
\Locale::setDefault('de_AT');

$this->assertSame(3655, (int) $transformer->reverseTransform('36,55'));
}

public function testFloatToIntConversionMismatchOnTransform()
{
$transformer = new MoneyToLocalizedStringTransformer(null, null, MoneyToLocalizedStringTransformer::ROUND_DOWN, 100);
IntlTestHelper::requireFullIntl($this, false);
\Locale::setDefault('de_AT');

$this->assertSame('10,20', $transformer->transform(1020));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ private function round($value, $precision)
} elseif (isset(self::$customRoundingList[$roundingModeAttribute])) {
$roundingCoef = pow(10, $precision);
$value *= $roundingCoef;
$value = (float) (string) $value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why first casting to string and then to float again ? The *= operator means that this is probably not a string already. What happens if you remove this ?


switch ($roundingModeAttribute) {
case self::ROUND_CEILING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ public function formatRoundingModeRoundHalfUpProvider()
// array(1.125, '1.13'),
array(1.127, '1.13'),
array(1.129, '1.13'),
array(1020 / 100, '10.20'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not 10.2 rather than a division ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof that's the issue, 1020 / 100 != 10.2, the actual value is 10.19999999998

You can test this example:

echo floor(1020 / 100 * 100); // 1019
echo floor((float) (string) (1020 / 100 * 100)); // 1020

See the original PR with this sort of fix #24036.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about that example? https://3v4l.org/5ZV3F does not confirm your observation. Should we use another example instead that allows to actually catch potential future regressions?

Copy link
Contributor Author

@syastrebov syastrebov May 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xabbuh yes, you forgot to add floor().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, thanks

);
}

Expand All @@ -451,6 +452,7 @@ public function formatRoundingModeRoundHalfDownProvider()
array(1.125, '1.12'),
array(1.127, '1.13'),
array(1.129, '1.13'),
array(1020 / 100, '10.20'),
);
}

Expand All @@ -474,6 +476,7 @@ public function formatRoundingModeRoundHalfEvenProvider()
array(1.125, '1.12'),
array(1.127, '1.13'),
array(1.129, '1.13'),
array(1020 / 100, '10.20'),
);
}

Expand All @@ -498,6 +501,7 @@ public function formatRoundingModeRoundCeilingProvider()
array(-1.123, '-1.12'),
array(-1.125, '-1.12'),
array(-1.127, '-1.12'),
array(1020 / 100, '10.20'),
);
}

Expand All @@ -522,6 +526,7 @@ public function formatRoundingModeRoundFloorProvider()
array(-1.123, '-1.13'),
array(-1.125, '-1.13'),
array(-1.127, '-1.13'),
array(1020 / 100, '10.20'),
);
}

Expand All @@ -546,6 +551,7 @@ public function formatRoundingModeRoundDownProvider()
array(-1.123, '-1.12'),
array(-1.125, '-1.12'),
array(-1.127, '-1.12'),
array(1020 / 100, '10.20'),
);
}

Expand All @@ -570,6 +576,7 @@ public function formatRoundingModeRoundUpProvider()
array(-1.123, '-1.13'),
array(-1.125, '-1.13'),
array(-1.127, '-1.13'),
array(1020 / 100, '10.20'),
);
}

Expand Down