[issue46183] float function errors on negative number string with comma seperator ex: '-1, 234.0'
Steven D'Aprano
report at bugs.python.org
Sun Dec 26 11:31:25 EST 2021
Steven D'Aprano <steve+python at pearwood.info> added the comment:
The behaviour is correct and not a bug. Commas are not supported when converting strings to float.
The allowed format for floats as strings is described in the docs:
https://docs.python.org/3/library/functions.html#float
By the way, the negative sign is irrelevant. Commas are not supported regardless of whether there is a negative sign or not.
The difficulty with commas is that it is ambiguous whether float('1,234') should interpret the comma as British/American digit grouping separator, and return 1234.0, or as the European decimal point, and return 1.234. For good or bad, Python has a bias towards English (like most programming languages) and so it chooses to only recognise the decimal point as a dot in the British/American style, and reject the comma.
If you want to support European-style commas as the decimal point, the easiest way is to call float('1,234'.replace(',', '.'))
----------
nosy: +steven.daprano
resolution: -> not a bug
stage: -> resolved
status: open -> closed
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46183>
_______________________________________
More information about the Python-bugs-list
mailing list