-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
string formatting that produces floats with preset precision while respecting locale #77912
Comments
.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. |
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. |
So far, I've implemented this for Decimal |
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. |
@rhettinger See bpo-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. |
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' |
Individually localizing numbers is not a viable solution to this problem. str.format() allows us to efficiently produce strings with lots of numbers...but only if you don't need localization. Localizing 10 or 20 numbers individually is verbose, inefficient and simply bad style. Python should allow users to use the full power of str.format() in a localized manner. |
Thanks everyone for their input, but I'm going to close this issue. Given that there's a workaround to use the |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: