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

Conversation

syastrebov
Copy link
Contributor

@syastrebov syastrebov commented Apr 3, 2018

Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets no
License MIT
Doc PR

Related issue #21026.
Previous PR #24036.
Similar fix for transform() method.

@syastrebov syastrebov force-pushed the number-formatter-round-fix branch from 5dff4ee to 5653b4e Compare April 3, 2018 20:40
@syastrebov syastrebov changed the title [WIP] fix rounding from string [WIP] Fix precision of MoneyToLocalizedStringTransformer's divisions on transform() Apr 3, 2018
@syastrebov syastrebov force-pushed the number-formatter-round-fix branch 4 times, most recently from 05c0aba to 65e0b29 Compare April 3, 2018 22:32
@syastrebov syastrebov changed the title [WIP] Fix precision of MoneyToLocalizedStringTransformer's divisions on transform() Fix precision of MoneyToLocalizedStringTransformer's divisions on transform() Apr 3, 2018
@syastrebov syastrebov force-pushed the number-formatter-round-fix branch from 65e0b29 to bddc80d Compare April 3, 2018 22:43
@syastrebov syastrebov changed the title Fix precision of MoneyToLocalizedStringTransformer's divisions on transform() [Form] Fix precision of MoneyToLocalizedStringTransformer's divisions on transform() Apr 3, 2018
@@ -710,6 +710,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 ?

@@ -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

@nicolas-grekas nicolas-grekas added this to the 2.8 milestone Apr 4, 2018
@fabpot
Copy link
Member

fabpot commented Apr 20, 2018

Don't we have the same issue in 2.7?

@xabbuh xabbuh added the Form label Apr 20, 2018
@xabbuh xabbuh modified the milestones: 2.8, 2.7 Apr 20, 2018
@xabbuh
Copy link
Member

xabbuh commented Apr 20, 2018

I think so. #24036 was merged into 2.7 as well.

@syastrebov syastrebov force-pushed the number-formatter-round-fix branch from b25e32b to f94b7aa Compare April 20, 2018 10:23
@syastrebov syastrebov changed the base branch from 2.8 to 2.7 April 20, 2018 10:23
@syastrebov
Copy link
Contributor Author

done

@fabpot
Copy link
Member

fabpot commented May 17, 2018

Thank you @syastrebov.

@fabpot fabpot merged commit f94b7aa into symfony:2.7 May 17, 2018
fabpot added a commit that referenced this pull request May 17, 2018
…s divisions on transform() (syastrebov)

This PR was merged into the 2.7 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | no
| License       | MIT
| Doc PR        |

Related issue #21026.
Previous PR #24036.
Similar fix for `transform()` method.

Commits
-------

f94b7aa fix rounding from string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants