[New-bugs-announce] [issue34311] locale.format() and locale.format_string() cast Decimals to float

James Emerton report at bugs.python.org
Wed Aug 1 16:22:51 EDT 2018

New submission from James Emerton <james at emerton.info>:

We use locale.format('%.2f', x, True) to convert Decimal values to strings for display. Unfortunately, the locale module is using %-formatting to generate the initial string before applying locale specific formatting. As a result, any value which cannot be accurately represented as a float will produce incorrect results.

I've built some formatting that uses new-style string formatting (and some internal locale functions) which corrects the problem.

Unfortunately, making this change in the locale module would require converting the input format string to the new syntax, so '%.2f' would become '{:.2f}'.

See also #33731

components: Library (Lib)
messages: 322885
nosy: jemerton
priority: normal
severity: normal
status: open
title: locale.format() and locale.format_string() cast Decimals to float
versions: Python 3.6, Python 3.7, Python 3.8

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list