Skip to content

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

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

Open
skirpichev opened this issue Feb 28, 2025 · 0 comments
Labels
docs Documentation in the Doc dir extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@skirpichev
Copy link
Member

skirpichev commented Feb 28, 2025

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

@skirpichev skirpichev added docs Documentation in the Doc dir extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Feb 28, 2025
@skirpichev skirpichev self-assigned this Feb 28, 2025
skirpichev added a commit to skirpichev/cpython that referenced this issue Mar 10, 2025
skirpichev added a commit to skirpichev/cpython that referenced this issue Mar 11, 2025
skirpichev added a commit to skirpichev/cpython that referenced this issue Apr 6, 2025
```pycon
>>> from _decimal import Decimal as D
>>> format(D(1234567), '_')
'1_234_567'
>>> format(D(1234567), '020_')
'0_000_000_001_234_567'
>>> format(D('1234.56'), '07_')
'1_234.56'
>>> format(D('1.23456789'), '_')
'1.23456789'
>>> format(D('123.456789'), '_%')
'12_345.6789%'
>>> from _pydecimal import Decimal as D
>>> format(D(1234567), '_')
'1_234_567'
>>> format(D(1234567), '020_')
'0_000_000_001_234_567'
>>> format(D('1234.56'), '07_')
'1_234.56'
>>> format(D('1.23456789'), '_')
'1.23456789'
>>> format(D('123.456789'), '_%')
'12_345.6789%'
```
@skirpichev skirpichev removed their assignment Apr 6, 2025
@skirpichev skirpichev removed the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Status: Todo
Development

No branches or pull requests

1 participant