GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Undocumented handling of thousands separator for floats and differences wrt Decimal · Issue #130664 · python/cpython

Undocumented handling of thousands separator for floats and differences wrt Decimal #130664

New issue
Copy link
New issue
Copy link
Open
Open
Undocumented handling of thousands separator for floats and differences wrt Decimal#130664
Copy link
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dirextension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@skirpichev
skirpichev
opened on Feb 28, 2025
Issue body actions

Bug report

Bug description:

In docs we have:

The ',' option signals the use of a comma for a thousands separator for floating-point presentation types and for integer presentation type 'd'. For other presentation types, this option is an error. For a locale aware separator, use the 'n' integer presentation type instead.

The '_' option signals the use of an underscore for a thousands separator for floating-point presentation types and for integer presentation type 'd'. For integer presentation types 'b', 'o', 'x', and 'X', underscores will be inserted every 4 digits. For other presentation types, specifying this option is an error.

But this doesn't specify how separators are inserted for customized alignment, consider:

>>> format(0.0, '>020_f')
'0000000000000.000000'
>>> format(0.0, '<020_f')
'0.000000000000000000'
>>> format(0.0, '=020_f')
'0_000_000_000.000000'
>>> format(0.0, '^020_f')
'0000000.000000000000'

Probably it's obvious, that separators might be inserted only in case of 0-padding and = alignment, though maybe this should be mentioned explicitly.

Note also that neither from above supported by Decimal/Fraction, e.g.:

>>> format(Decimal(0.0), '=020_f')
Traceback (most recent call last):
  File "<python-input-13>", line 1, in <module>
    format(Decimal(0.0), '=020_f')
    ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid format string
>>> format(Fraction(0.0), '=020_f')
'            0.000000'
>>> format(float(Fraction(0.0)), '=020_f')
'0_000_000_000.000000'

BTW, the rationale for Fraction's behavior (see #100161) is "no special-casing of the particular '0=' fill-character/alignment combination", which is rather weak, IMO.

Either this should be fixed (preferred) or documented.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

  • gh-130664: specify how thousands separators are used with 0-padding #131030
  • gh-130664: treat '0' fill character with align '=' as zero-padding for Fraction's #131067
  • gh-130664: support '_' (just as ',') in Decimal's formatting #132155
  • [3.14] gh-130664: Treat '0' fill character with align '=' as zero-padding for Fraction's (GH-131067) #136241
  • [3.13] gh-130664: Treat '0' fill character with align '=' as zero-padding for Fraction's (GH-131067) #136242
  • [3.13] gh-130664: support '_' (just as ',') in Decimal's formatting (GH-132155) #136649

Activity

  1. skirpichev
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Feb 28, 2025
  2. skirpichev
    self-assigned this
    on Feb 28, 2025
  3. github-project-automation
    added this to docs issueson Feb 28, 2025
  4. github-project-automation
    moved this to Todo in docs issueson Feb 28, 2025
  5. skirpichev
    added 2 commits that reference this issue on Mar 10, 2025

    pythongh-130664: specify how thousands separators are used with 0-pad…

    d26c426

    pythongh-130664: treat '0' fill character with align '=' as zero-padd…

    d46ca41
  6. skirpichev
    added a commit that references this issue on Apr 6, 2025

    pythongh-130664: support '_' (just as ',') in Decimal's formatting

    1ae18f5
  7. skirpichev
    removed their assignment
    on Apr 6, 2025
  8. skirpichev
    removed
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Apr 6, 2025
  9. ambv
    added a commit that references this issue on Apr 15, 2025

    gh-130664: support '_' (just as ',') in Decimal's formatting (#132155)

    e10fe81
  10. serhiy-storchaka commented on Jul 3, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Jul 3, 2025
    Member
    More actions

    There is also a wart: specifying the thousands separator with zero padding can increase the length of the resulting string over the specified width:

    >>> format(1, '04')
    '0001'
    >>> format(1, '04,')
    '0,001'
    >>> format(1, '4,')
    '   1'
  11. serhiy-storchaka
    added a commit that references this issue on Jul 3, 2025

    gh-130664: Treat '0' fill character with align '=' as zero-padding fo…

    c113a8e
  12. miss-islington
    added 2 commits that reference this issue on Jul 3, 2025

    pythongh-130664: Treat '0' fill character with align '=' as zero-padd…

    7f65738

    pythongh-130664: Treat '0' fill character with align '=' as zero-padd…

    3a9fd7f
  13. serhiy-storchaka
    added 2 commits that reference this issue on Jul 3, 2025

    [3.13] gh-130664: Treat '0' fill character with align '=' as zero-pad…

    c30178a

    [3.14] gh-130664: Treat '0' fill character with align '=' as zero-pad…

    4877f43
  14. skirpichev commented on Jul 4, 2025

    @skirpichev
    skirpichev
    on Jul 4, 2025
    MemberAuthor
    More actions

    specifying the thousands separator with zero padding can increase the length of the resulting string over the specified width

    @serhiy-storchaka, I think we shouldn't add anything (after #131030). Docs says:

    The width is a decimal integer defining the minimum total field width, including any prefixes, separators, and other formatting characters.

    IMO, it's obvious, that as separators are placed between zeros (i.e. we can't start with '_' or ',') - the output string can be bigger from that.

    So, #131030 should finally fix issue.

  15. AndPuQing
    added a commit that references this issue on Jul 11, 2025

    pythongh-130664: Treat '0' fill character with align '=' as zero-padd…

    26b57a7
  16. Pranjal095
    added a commit that references this issue on Jul 12, 2025

    pythongh-130664: Treat '0' fill character with align '=' as zero-padd…

    6dbc3ff
  17. picnixz
    added a commit that references this issue on Jul 13, 2025

    pythongh-130664: Treat '0' fill character with align '=' as zero-padd…

    aec587f
  18. miss-islington
    added a commit that references this issue on Jul 14, 2025

    pythongh-130664: support '_' (just as ',') in Decimal's formatting (p…

    26be90d
  19. picnixz
    added a commit that references this issue on Jul 14, 2025

    [3.13] gh-130664: support '_' (just as ',') in Decimal's formatting (G…

    2a6617d
  20. picnixz
    marked , works with Decimal() against the doc of **Format Specification Mini-Language** #136636 as a duplicate of this issue on Jul 14, 2025
  21. taegyunkim
    added a commit that references this issue on Aug 4, 2025

    pythongh-130664: Treat '0' fill character with align '=' as zero-padd…

    b4aac30
  22. Agent-Hellboy
    added a commit that references this issue on Aug 19, 2025

    pythongh-130664: Treat '0' fill character with align '=' as zero-padd…

    d2d1eba
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

    docsDocumentation in the Doc dirDocumentation in the Doc dirextension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • docs issues
      Status
      Todo
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.