Closed agitator closed 4 years ago
self.formatter
is defined as
locale = self.widget.request.locale
self.formatter = locale.numbers.getFormatter('decimal')
So it might probably be helpful to know what locale you're in and what the formatter winds up being.
(Pdb++) locale.getLocaleID()
'de'
so the fix should be in a po-file?
hmm but either way it should convert it the same way
zope.i18n's 'de' locale uses '.' as the group separator, so that much appears consistent:
>>> from zope.i18n import locales
>>> de = locales.locales.getLocale('de')
>>> de.numbers.getFormatter('decimal').getPattern()
'#,##0.###;-#,##0.###'
>>> de_f = de.numbers.getFormatter('decimal')
>>> de_f.symbols
{'decimal': ',',
'exponential': 'E',
'group': '.',
'infinity': '∞',
'list': ';',
'minusSign': '-',
'nan': '�',
'nativeZeroDigit': '0',
'patternDigit': '#',
'perMille': '‰',
'percentSign': '%',
'plusSign': '+'}
>>> de.numbers.getFormatter('decimal').parse('1.7641597028783658')
17641597028783658
>>> de.numbers.getFormatter('decimal').format(17641597028783658)
'17.641.597.028.783.658'
I don't know where 19000000000000000.00
is coming from, it looks like rounding.
The English locale appears to share the same format string:
>>> de.numbers.getFormatter('decimal').getPattern()
'#,##0.###;-#,##0.###'
>>> en = locales.locales.getLocale('en')
>>> en.numbers.getFormatter("decimal").getPattern()
'#,##0.###;-#,##0.###'
But they differ in their symbols:
>>> de_f.symbols['decimal']
','
>>> en_f.symbols['decimal']
'.'
The english locale won't parse the number; it won't parse it because of too many decimal digits (too much precision) as 3 digits appears to be the most the pattern allows:
>>> en.numbers.getFormatter('decimal').parse('1.7641597028783658')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "//python3.8/site-packages/zope/i18n/format.py", line 315, in parse
raise NumberParseError('Not a valid number for this pattern %r.'
zope.i18n.format.NumberParseError: Not a valid number for this pattern '#,##0.###;-#,##0.###'.
>>> en.numbers.getFormatter("decimal").parse('17641597028783.658')
17641597028783.656
It does format it as I would expect, in the same way the de format does:
>>> en.numbers.getFormatter('decimal').format(17641597028783658)
'17,641,597,028,783,658'
Yes, 19000000000000000.00
is from rounding and some taxes in the view
Copying over a value osx (locale en) calculator to plone site (locale de)
toFieldValue: 1.5
FormatterParseToFieldValue: 15
toFieldValue: 1,5
FormatterParseToFieldValue: 1.5
So it seems to be the wrong decimal delimiter... Everything works then as expected... WOW!
Thanx @jamadden for the enlightenment 👍
Maybe it would be an idea to simplify that code? Get the first nonnumerical character from the right side as delimiter and delete all grouping characters?
That's not always what you want:
1.234.567
should become Decimal('1234567')
with locale='de'
, not Decimal('1.234567')
as this would be more than surprising for German natives.
I'd suggest to close this issue and leave the code as it is. Okay?
@icemac you're right Thx again for all the feedback!
Plone 5.2 on Py 3.7
I have a behavior with a decimal field
Enter
1.7641597028783658
View19000000000000000.00
Reedit17.641.597.028.783.658
Stepping into
NumberDataConverter
Can someone give me a hint why this is happening? Or any pointer on how to fix?