Visitar URL original
Undocumented handling of thousands separator for floats and differences wrt Decimal · Issue #130664 · python/cpython · GitHub
Skip to content

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

Description

@skirpichev

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

Activity

  1. added
    docsDocumentation in the Doc dir
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on Feb 28, 2025
  2. self-assigned this
    on Feb 28, 2025
  3. added 2 commits that reference this issue on Mar 10, 2025
  4. added a commit that references this issue on Apr 6, 2025
  5. removed their assignment
    on Apr 6, 2025
  6. added a commit that references this issue on Apr 15, 2025
  7. serhiy-storchaka commented on Jul 3, 2025

    @serhiy-storchaka
    Member

    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'
  8. added a commit that references this issue on Jul 3, 2025
  9. added 2 commits that reference this issue on Jul 3, 2025
  10. added 2 commits that reference this issue on Jul 3, 2025
  11. skirpichev commented on Jul 4, 2025

    @skirpichev
    MemberAuthor

    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.

  12. added a commit that references this issue on Jul 11, 2025
  13. added a commit that references this issue on Jul 12, 2025
  14. added a commit that references this issue on Jul 13, 2025
  15. added a commit that references this issue on Jul 14, 2025
  16. added a commit that references this issue on Jul 14, 2025
  17. added a commit that references this issue on Aug 4, 2025
  18. added a commit that references this issue on Aug 19, 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

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

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions