This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: string formatting that produces floats with preset precision while respecting locale
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: Jakub Szewczyk, ced, eric.smith, jemerton, rhettinger, skrah
Priority: normal Keywords: patch

Created on 2018-06-01 12:42 by Jakub Szewczyk, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 8612 closed python-dev, 2018-08-02 03:34
Messages (6)
msg318407 - (view) Author: Jakub Szewczyk (Jakub Szewczyk) Date: 2018-06-01 12:42
.2f produces a string representation of a float rounded up to 2 significant digits.

>>> print ("{:.2f}".format(1.891))
1.89

However, it does not respect locale. There is no counterpart of 'f' that would respect locale. There is 'n', but because it follows the rules of 'g', in many cases it returns a different number of significant digits. 

>>> print ("{:.2n}".format(1.891))
1.9


In all my uses of formatted float printing, I need to produce floats that are rounded to have the same number of significant digits. I _presume_ this generalizes to the majority people, and the use of 'f' option is much more widespread than the use of 'g'. If this is the case, then a locale-friendly counterpart of 'f' would be very useful.
msg318410 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-06-01 13:12
You can always use the locale module, although of course that's not as convenient:

>>> locale.format('%.2f', 1.891)
'1.89'

I'm open to suggests on backward compatible ways to implement this for python 3.8. It would probably involve a new letter, and need to be implemented for at least int, float, decimal, and complex.
msg322911 - (view) Author: James Emerton (jemerton) * Date: 2018-08-02 03:42
So far, I've implemented this for Decimal
msg322913 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-08-02 04:26
I would like to see locale() remain somewhat decoupled from the rest of string formatting.  IIRC locale() is finicky especially when concurrency is involved.  I think a best practice is for an app to address locale issues explicitly rather than inside string formatting.  I'll defer to those with more experience using locale -- I just have a vague recollection of this being an area fraught with landmines.
msg322914 - (view) Author: James Emerton (jemerton) * Date: 2018-08-02 04:31
@rhettinger See #34311 about formatting Decimals using locale.format(). I'd like to see the problem fixed in one place or the other.

Also, this is seems relatively straightforward to implement as it's really just a combination of the fixed precision 'f' and the locale specific 'n' format types.
msg349684 - (view) Author: Cédric Krier (ced) * Date: 2019-08-14 11:34
I think PR-15275 will solves this issue also as you could use:

>>> locale.setlocale(locale.LC_ALL, 'fr_FR')
>>> locale.localize('{:.2f}'.format(1.891))
'1,89'
History
Date User Action Args
2022-04-11 14:59:01adminsetgithub: 77912
2019-08-14 11:34:15cedsetnosy: + ced
messages: + msg349684
2018-08-04 21:47:48skrahsetnosy: + skrah
2018-08-02 04:31:21jemertonsetmessages: + msg322914
2018-08-02 04:26:08rhettingersetnosy: + rhettinger
messages: + msg322913
2018-08-02 03:42:26jemertonsetmessages: + msg322911
2018-08-02 03:34:56python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request8118
2018-08-02 00:48:53jemertonsetnosy: + jemerton
2018-06-01 13:12:10eric.smithsetassignee: eric.smith
messages: + msg318410
components: + Interpreter Core, - Extension Modules
versions: + Python 3.8, - Python 3.6
2018-06-01 12:42:33Jakub Szewczykcreate