[issue2551385] Use locale aware conversions for string -> float.
John Rouillard <[email protected]> Wed, 01 Jan 2025 19:23:57 +0000
| Newsgroups | gmane.comp.bug-tracking.roundup.devel |
|---|---|
| Message-ID | <[email protected]> |
New submission from John Rouillard:
Issue1182919 (support range syntax for numbers), has a discussion of representing
a floating point number using "12345,24" where the comma is the decimal separator.
This localized representation doesn't seem to work as
hyperdb.py::Number.from_raw(self, value, *kw)
uses float() to convert value to a number. Float isn't locale aware.
>>> string_number = "1234,56"
>>> float(string_number)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '1234,56'
Also it doesn't support ',' or '.' as thousand separators (but does support '_' for
thousand separators by python 3.8.
This conversion needs to use something like:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "en_DK.utf8")
'en_DK.utf8'
>>> string_number = "1.234,56"
>>> float_number = locale.atof(string_number)
>>> print(float_number)
1234.56
which does support alternate thousand separators and decimal separators according
to the locale. (However thousand separators with exponent: locale.atof("1,234.56E3")
generates a ValueError in en_US but locale.atof("1.234,56E-2") generates (incorrectly)
1234.56 with en_HK (python 3.8.10).)
Currently the hyperdb is locale unaware. Also locale methods are not
thread safe (which might be an issue for roundup-server in thread mode). Some backends
like back_anydbm use float() directly for checks/conversions.
I am documenting the number input type in user_guide.txt as requiring a period
decimal separator, no thousands and supporting scientific notation.
----------
components: Database
keywords: Effort-High
messages: 8247
nosy: rouilj
severity: normal
status: new
title: Use locale aware conversions for string -> float.
type: behavior
_________________________________________________
Roundup tracker <[email protected]>
<https://issues.roundup-tracker.org/issue2551385>
_________________________________________________