Skip to content

Introduce fixed point locale aware format type for floating point numbers #79819

Closed as not planned

Description

@steelman
mannequin
BPO 35638
Nosy @rhettinger, @mdickinson, @vstinner, @ericvsmith, @skrah, @serhiy-storchaka, @steelman, @huftis
PRs
  • gh-79819: Introduce fixed point locale aware format type #11405
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2019-01-02.11:25:01.876>
    labels = ['interpreter-core', 'type-feature', '3.9']
    title = 'Introduce fixed point locale aware format type for floating point numbers'
    updated_at = <Date 2019-09-16.08:15:57.874>
    user = 'https://github.com/steelman'

    bugs.python.org fields:

    activity = <Date 2019-09-16.08:15:57.874>
    actor = 'vstinner'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2019-01-02.11:25:01.876>
    creator = 'steelman'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35638
    keywords = ['patch', 'patch', 'patch', 'patch']
    message_count = 18.0
    messages = ['332863', '332876', '332877', '332878', '332879', '332880', '332890', '332928', '332930', '332931', '332932', '333000', '333052', '333270', '333296', '333297', '333315', '352492']
    nosy_count = 8.0
    nosy_names = ['rhettinger', 'mark.dickinson', 'vstinner', 'eric.smith', 'skrah', 'serhiy.storchaka', 'steelman', 'huftis']
    pr_nums = ['11405']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue35638'
    versions = ['Python 3.9']

    Activity

    1. steelman commented on Jan 2, 2019

      steelmanmannequin
      MannequinAuthor

      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.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Jan 2, 2019
    3. ericvsmith commented on Jan 2, 2019

      @ericvsmith
      Member

      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?

    4. added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      and removed
      stdlibStandard Library Python modules in the Lib/ directory
      on Jan 2, 2019
    5. steelman commented on Jan 2, 2019

      steelmanmannequin
      MannequinAuthor

      I've got the patch. I will push it to github as soon as I can (some technical issues).

    6. ericvsmith commented on Jan 2, 2019

      @ericvsmith
      Member

      Before a patch is created, we should discuss the behavior that will be implemented and agree on it. What is your suggestion?

    7. ericvsmith commented on Jan 2, 2019

      @ericvsmith
      Member

      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.

    8. steelman commented on Jan 2, 2019

      steelmanmannequin
      MannequinAuthor

      I have created a new format "m" that is for "n", what "f" is for "g". The patch for string.rst says

      +---------+----------------------------------------------------------+
      | 'm' | Number. This is the same as 'f', except that it uses |
      | | the current locale setting to insert the appropriate |
      | | number separator characters. |
      +---------+----------------------------------------------------------+

      My patch only applies to floats not integers.

    9. ericvsmith commented on Jan 2, 2019

      @ericvsmith
      Member

      I haven't looked at this closely yet, but you'll need to at least:

      • add tests that the locale-aware formatting is happening
      • support decimal
      • make sure it works with complex (which it probably does, but needs a test)

      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.

    10. changed the title [-]Introduce fixed point locale awear format type for floating point numbers[/-] [+]Introduce fixed point locale aware format type for floating point numbers[/+] on Jan 2, 2019
    11. 4 remaining items

    12. serhiy-storchaka commented on Jan 4, 2019

      @serhiy-storchaka
      Member

      You can use locale.format_string() for locale aware formatting.

    13. steelman commented on Jan 5, 2019

      steelmanmannequin
      MannequinAuthor

      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.

    14. vstinner commented on Jan 9, 2019

      @vstinner
      Member

      Łukasz Stelmach:

      It is currently impossible to format floating point numbers with an arbitrary number of decimal digits AND the decimal point matching locale settings.

      I would like to warn you that handling properly locales can be very tricky. I just wrote an article about that:
      https://vstinner.github.io/locale-bugfixes-python3.html

      Stefan Krah:

      My main concern with 'm' for libmpdec is that I'd like to reserve it
      for LC_MONETARY.

      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?

      • decimal point
      • thousands separator
      • grouping

      Because there are more and more format variants. See for example Python/formatter_unicode.c. It has 5 "locale types":

      • LT_NO_LOCALE
      • LT_DEFAULT_LOCALE
      • LT_UNDERSCORE_LOCALE
      • LT_UNDER_FOUR_LOCALE
      • LT_CURRENT_LOCALE

      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.

    15. skrah commented on Jan 9, 2019

      skrahmannequin
      Mannequin

      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?

      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?

      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.

      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.

    16. steelman commented on Jan 9, 2019

      steelmanmannequin
      MannequinAuthor

      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

    17. vstinner commented on Jan 9, 2019

      @vstinner
      Member

      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.

      FYI I opened bpo-35697 to discuss the decimal module case.

    18. rhettinger commented on Sep 15, 2019

      @rhettinger
      Contributor

      I had thought that use a locales were deemed an anti-pattern (not easy-to-use, not thread-safe, etc).

    19. transferred this issue fromon Apr 10, 2022
    20. encukou commented on Sep 2, 2025

      @encukou
      Member

      The mailing list thread gathered some +1 for a modifier rather than a new type, but then died down.
      There is a workaround available: locale.format_string.
      At this point, the discussion should be restarted on Discourse if anyone wants to push this forward.

      I'll close the issue in a few days if there are no objections.

    21. added
      pendingThe issue will be closed if no feedback is provided
      on Sep 2, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.9 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedtype-featureA feature request or enhancement

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions