Creating python decimal with locale formatted string

Question:

I am using the Python decimal module, I’ve set my locale to Germany such that locale.localeconv()["decimal_point"] is ",". When I try creating a decimal like decimal.Decimal("1,23"), I get a decimal conversion syntax error. When I do decimal.Decimal("1.23"), it works.

Does the Python decimal not respect locale settings when creating decimal from string?

Asked By: Wirable2323

||

Answers:

No, the decimal.Decimal constructor only accepts numeric strings according to the grammar shown, where . is the only decimal point supported.

decimal-part   ::=  digits '.' [digits] | ['.'] digits

The locale settings are only relevant for formatting a Decimal back to a string, see e.g. How can I locale-format a python Decimal and preserve its precision?

Answered By: mkrieger1
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.