Issue 33731: string formatting that produces floats with preset precision while respecting locale (original) (raw)

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:01 admin set github: 77912
2019-08-14 11:34:15 ced set nosy: + cedmessages: +
2018-08-04 21:47:48 skrah set nosy: + skrah
2018-08-02 04:31:21 jemerton set messages: +
2018-08-02 04:26:08 rhettinger set nosy: + rhettingermessages: +
2018-08-02 03:42:26 jemerton set messages: +
2018-08-02 03:34:56 python-dev set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest8118>
2018-08-02 00:48:53 jemerton set nosy: + jemerton
2018-06-01 13:12:10 eric.smith set assignee: eric.smithmessages: + components: + Interpreter Core, - Extension Modulesversions: + Python 3.8, - Python 3.6
2018-06-01 12:42:33 Jakub Szewczyk create