-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
MoneyType with divisor: integer conversion leads to wrong database values #21026
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
Comments
I would vote to That's what I did for RollerworksSearch after almost killing myself with this logic... Edit. In my case I actually had to increment the whole amount (+1.00) and reset the cents, but even then the numbers did not always work as expected. |
That would be a hard option and definitely raises a fat BC issue ;) |
Yeah it would be a BC break, but it would solve this issue properly and probably once and for all. |
This one's fixed by #24036 ! FINALLY! Thank you so much 😘 |
Closing then |
…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
…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 symfony/symfony#21026. Previous PR symfony/symfony#24036. Similar fix for `transform()` method. Commits ------- f94b7aadd3 fix rounding from string
http://stackoverflow.com/questions/41286474/symfony2-moneytype-with-divisor-integer-conversion-leads-to-wrong-database-valu
We're storing all our money related values as cents in our database (ODM but ORM will likely behave the same). We're using
MoneyType
to convert user facing values (12,34€) into their cents representation (1234c). The typical float precision problem arises here: due to insufficient precision there are many cases that create rounding errors that are merely visible when debugging.MoneyType
will convert incoming strings to floats that may be not precise ("1765" => 1764.9999999998). Things get bad as soon as you persist these values. The (integer) cast in IntType (ODM/ORM) will strip off the value's mantissa instead of rounding the value, effectively leading to writing wrong values into the database (1764 instead of 1765 when "1765" is internally 1764.9999999998).Here's a unit test that should work from within any Symfony/container application:
might relate to #10240 and #8473
The text was updated successfully, but these errors were encountered: