-
-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Introduce fixed point locale aware format type for floating point numbers #79819
Comments
It is currently impossible to format floating point numbers with an arbitrary number of decimal digits AND the decimal point matching locale settings. For example no current format allows to display numbers ranging from 1 to 1000 with exactly two decimal digits. |
Since this is a new feature, it can only be added to 3.8. Adjusting versions accordingly. I suggest that if we add this at all, it only be added to __format__, not to %-formatting. Any suggestions on a specification for this? |
I've got the patch. I will push it to github as soon as I can (some technical issues). |
Before a patch is created, we should discuss the behavior that will be implemented and agree on it. What is your suggestion? |
Of course, feel free to create a PR. But the correct place to discuss any new behavior is on the issue tracker, or maybe on python-ideas, not in a PR. |
I have created a new format "m" that is for "n", what "f" is for "g". The patch for string.rst says +---------+----------------------------------------------------------+ My patch only applies to floats not integers. |
I haven't looked at this closely yet, but you'll need to at least:
And, I think we'll need to run this through python-ideas first. One thing I expect to come up there: why f and not g? Again, I haven't looked through the code yet, or really even given any thought to determining if this is a sound idea. |
Done.
Good points. Done. Please note, that there is an inconsistency between float/complex/int/_pydecimal(!) and decimal. The former provide only 'n' format type and the latter provides 'n' and 'N'. So I implemented 'm' and 'M' for decimal and 'm' for _pydecimal.
There are no tests for 'n'. Should I create for both 'm' and 'n'?
Because 'g' has been already covered with 'n'. |
I think there's another open GitHub issue for this, and yes, probably My main concern with 'm' for libmpdec is that I'd like to reserve it On the other hand perhaps '$' would also be possible for monetary. So it appears that there might be some bikeshedding about the names |
As much as I am open to any suggestions for naming and such (although I think 'm' together with 'n' are a good supplement for 'f' and 'g'), I really would like to introduce a method to format numbers with fixed number of decimal digits (it looks good in tables) and with separators from locale. |
For reference, the (one of the?) other GitHub issue(s) is here: It actually proposes to use LC_MONETARY. |
You can use locale.format_string() for locale aware formatting. |
Indeed. Thank you. I was sure I had tried this. However, this is still only a workaround and not the solution I need. I am working on a project now which uses pint https://pint.readthedocs.io/en/latest/ which uses format() and its relatives. With "n" format present Python is missing locale-aware "f" formatter anyway. |
Łukasz Stelmach:
I would like to warn you that handling properly locales can be very tricky. I just wrote an article about that: Stefan Krah:
Since it seems like we are still at the "idea" stage, would it make sense to add a function which accept options to choose how to format a number?
Because there are more and more format variants. See for example Python/formatter_unicode.c. It has 5 "locale types":
and it uses this structure: /* Locale info needed for formatting integers and the part of floats
before and including the decimal. Note that locales only support
8-bit chars, not unicode. */
typedef struct {
PyObject *decimal_point;
PyObject *thousands_sep;
const char *grouping;
char *grouping_buffer;
} LocaleInfo; There is the locale but also "underscore" separator for thousands: see PEP-515. I'm not talking about adding something into format(), but add a method to float maybe. Or add a function somewhere else. -- By the way, the decimal module doesn't support properly the following corner case: LC_NUMERIC using an encoding different than LC_CTYPE encoding. I wrote #5191 but I abandonned my change. |
Maybe, but I think for format() Eric's latest proposal on python-ideas is great ("*f" for "f + LC_NUMERIC", "$f" for "f + LC_MONETARY". For me that's sufficient. Does locale.format_string() handle the other cases?
Well, I *discovered and opened* bpo-7442 several years ago, and you said: "I see that various people contributed to the issue, but it looks like the only user asking for the request is Stefan Krah. I prefer to close the issue and wait until more users ask for it before considering again the patch, or find a different way to implement the feature (support LC_NUMERIC and LC_CTYPE locales using a different encoding)." So why would you think that I'm not aware of that issue? It has low priority for me and I hesitate to depend on the official locale functions in decimal because I don't want to be involved in additional issue reports in that area. |
I'd appreciate, if we continued the discussion at python-ideas, where I posted the idea[1]. There has already been several valuable comments. [1] https://mail.python.org/pipermail/python-ideas/2019-January/054793.html |
I had thought that use a locales were deemed an anti-pattern (not easy-to-use, not thread-safe, etc). |
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: