Home > Software design >  Creating python decimal with locale formatted string
Creating python decimal with locale formatted string

Time:10-30

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?

CodePudding user response:

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?

  • Related