mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Decimal to string conversion ignores system locale #6021
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @flaushi on GitHub (Jul 17, 2018).
Originally assigned to: @Ocramius on GitHub.
Support Question
I have to make sure to handle decimals correctly. I have an Entity with
decimaltype member.When assigning 1234.56 to it, after fetching the object, the value is the string "1234.56", so no thousands-separator and
.as decimal separator. The system locale isdeso the locale aware string should be "1.234,56".Can I rely on this locale-unaware return value? Can you point me to the source code where it happens, I can't find it.
I also posed this question in SO, but got no answer so far.
Thanks in advance!
@Ocramius commented on GitHub (Jul 17, 2018):
1.234,56is NOT a valid number. Yes, it is in DE, but not for computer systems.Type conversions in DBAL only convert via type casts, nothing locale-aware.
See
5405427568/lib/Doctrine/DBAL/Types/IntegerType.php (L52-L55)@flaushi commented on GitHub (Jul 17, 2018):
Thanks! For integers, everything is clear. For floats/decimals its diffferent. Looking at
5405427568/lib/Doctrine/DBAL/Types/DecimalType.php (L52), I see no conversion/casting being performed. Does this mean, that the result depends on what is returned from the database? The database however does not return a string...@Ocramius commented on GitHub (Jul 17, 2018):
Decimal types are supposed to be kept as
stringto avoid type juggling errors.@flaushi commented on GitHub (Jul 17, 2018):
So, then
floatvalis the correct way to get the corresponding float, independently of any locale? This makes things easier. I was just afraid that switching to another DB layer any time in future might break this rule and lead to errors then.@Ocramius commented on GitHub (Jul 17, 2018):
@flaushi yes, this is supposed to be locale-safe. Strongly advise against
(float)-ing anumericfield though: use something likeext-bcmathto ensure that operations make sense at the end of the day, and that you don't have an IEEE-754 rounding error.@flaushi commented on GitHub (Jul 17, 2018):
Thats a good catch, thank you. Issue can be closed