Repository navigation
Undocumented handling of thousands separator for floats and differences wrt Decimal #130664
Description
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
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsTodo
Activity
pythongh-130664: specify how thousands separators are used with 0-pad…
pythongh-130664: treat '0' fill character with align '=' as zero-padd…
pythongh-130664: support '_' (just as ',') in Decimal's formatting
gh-130664: support '_' (just as ',') in Decimal's formatting (#132155)
serhiy-storchaka commentedon Jul 3, 2025
There is also a wart: specifying the thousands separator with zero padding can increase the length of the resulting string over the specified width:
gh-130664: Treat '0' fill character with align '=' as zero-padding fo…
pythongh-130664: Treat '0' fill character with align '=' as zero-padd…
pythongh-130664: Treat '0' fill character with align '=' as zero-padd…
[3.13] gh-130664: Treat '0' fill character with align '=' as zero-pad…
[3.14] gh-130664: Treat '0' fill character with align '=' as zero-pad…
skirpichev commentedon Jul 4, 2025
@serhiy-storchaka, I think we shouldn't add anything (after #131030). Docs says:
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.
pythongh-130664: Treat '0' fill character with align '=' as zero-padd…
pythongh-130664: Treat '0' fill character with align '=' as zero-padd…
pythongh-130664: Treat '0' fill character with align '=' as zero-padd…
pythongh-130664: support '_' (just as ',') in Decimal's formatting (p…
[3.13] gh-130664: support '_' (just as ',') in Decimal's formatting (G…
,works withDecimal()against the doc of **Format Specification Mini-Language** #136636 as a duplicate of this issuepythongh-130664: Treat '0' fill character with align '=' as zero-padd…
pythongh-130664: Treat '0' fill character with align '=' as zero-padd…