8000 [Form] Fix precision of MoneyToLocalizedStringTransformer's divisions and multiplications by Rubinum · Pull Request #24036 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fix precision of MoneyToLocalizedStringTransformer's divisions and multiplications #24036

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
Sep 29, 2017
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
Added improvement for accuracy in MoneyToLocalizedStringTransformer.
  • Loading branch information
Rubinum committed Sep 26, 2017
commit ab47c7878ed9fc20884b10bc522fd07cf3369e48
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ public function __construct($scale = 2, $grouping = true, $roundingMode = self::
*/
public function transform($value)
{
if (null !== $value) {
if (null !== $value && 1 !== $this->divisor) {
if (!is_numeric($value)) {
throw new TransformationFailedException('Expected a numeric.');
}

$value /= $this->divisor;
$value = (string) ($value / $this->divisor);
Copy link
Member

Choose a reason for hiding this comment

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

This looks wrong to me. Previously, $value was a float here, but now it is a string.

Copy link
Contributor Author
@Rubinum Rubinum Sep 27, 2017

Choose a reason for hiding this comment

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

I changed it from (float) (string) ($value / $this->divisor); to (string) ($value / $this->divisor); because this functions purpose is to Transforms a normalized format into a localized money string. according to the PHPDoc-Block. The return type is also string, thats why I changed it. Should I put a float cast in front of it again?

Copy link
Contributor

Choose a reason for hiding this comment

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

that is fine. the parent transform should be able handle both string or float.

}

return parent::transform($value);
Expand All @@ -78,9 +77,8 @@ public function transform($value)
public function reverseTransform($value)
{
$value = parent::reverseTransform($value);

if (null !== $value) {
$value *= $this->divisor;
if (null !== $value && 1 !== $this->divisor) {
$value = (float) (string) ($value * $this->divisor);
}

return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ public function testReverseTransformEmpty()

$this->assertNull($transformer->reverseTransform(''));
}

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'));
}
}
0