Skip to content

, works with Decimal() against the doc of **Format Specification Mini-Language** #136636

Description

@hyperkai

Bug report

Bug description:

The doc of Format Specification Mini-Language doesn't say that , works with Decimal() as shown below:

',': Inserts a comma every 3 digits for integer presentation type 'd' and floating-point presentation types, excluding 'n'. For other presentation types, this option is not supported.

But , works with Decimal() against the doc as shown below:

from decimal import Decimal

v = Decimal(value='1234.5555555555')

print('"{:,.20f}"'.format(v))
print('"{:,.20F}"'.format(v))
# "1,234.55555555550000000000"
#        |        20        |

print('"{:,f}"'.format(v))
print('"{:,F}"'.format(v))
# "1,234.5555555555"
#        |   10   |
from decimal import Decimal

v = Decimal('123456.78912')

print('"{:,.20g}"'.format(v))
print('"{:,.20G}"'.format(v))
print('"{:,.20}"'.format(v))
print('"{:,g}"'.format(v))
print('"{:,G}"'.format(v))
print('"{:,}"'.format(v))
# "123,456.78912"
#  |    11     |

In addition, the doc doesn't say that _ works with Decimal() as shown below:

'_': Inserts an underscore every 3 digits for integer presentation type 'd' and floating-point presentation types, excluding 'n'. For integer presentation types 'b', 'o', 'x', and 'X', underscores are inserted every 4 digits. For other presentation types, this option is not supported.

So, _ doesn't work with Decimal() as the doc doesn't say so as shown below:

from decimal import Decimal

v = Decimal(value='1234.5555555555')

print('"{:_.20f}"'.format(v))
print('"{:_.20F}"'.format(v))
print('"{:_f}"'.format(v))
print('"{:_F}"'.format(v))
# ValueError: invalid format string
from decimal import Decimal

v = Decimal('123456.78912')

print('"{:_.20g}"'.format(v))
print('"{:_.20G}"'.format(v))
print('"{:_.20}"'.format(v))
print('"{:_g}"'.format(v))
print('"{:_G}"'.format(v))
print('"{:_}"'.format(v))
# ValueError: invalid format string

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Activity

  1. picnixz commented on Jul 13, 2025

    @picnixz
    Member

    I'm not sure I understand the first issue namely with ,. The grammar says:

    grouping ::= "," | "_"

    So , is supported as you observed (both for integer and floating-point presentations). As for _, do you mean that it doesn't work for any of the floating-point presentation types? I don't have access to a 3.13+ installation so I can't check. Also, in the future, please check the bugs against the stable versions, and not those that are security-only.

    cc @skirpichev

  2. added
    pendingThe issue will be closed if no feedback is provided
    on Jul 13, 2025
  3. skirpichev commented on Jul 14, 2025

    @skirpichev
    Member

    This is certainly not a bug. The Decimal formatting is documented on the quoted page, as it says that available presentation types for floats and Decimals are same.

    On the other hand, the Decimal is not a builtin type, maybe it's better to document it's formatting separately, as for Fractions, in the module page. (Right now it even has no examples of formatted output, see #128698.)

  4. added and removed
    pendingThe issue will be closed if no feedback is provided
    on Jul 14, 2025
  5. terryjreedy commented on Jul 14, 2025

    @terryjreedy
    Member

    The "floating-point presentation types" are the implicit or explict f,F,g,G, not the object type. I don't see why you are getting the error messages. Maybe a fixed bug. In installed 3.14.0b4 and main:

    >>> '{:_.20}'.format(d)
    '123_456.123456'
    >>> '"{:_.20}"'.format(d)
    '"123_456.123456"'
    

    Same with f-strings.

    EDIT: Mismatches between presentation type and object type are ValueErrors.

    >>> '{:_.20}'.format('453455')
    Traceback (most recent call last):
      File "<python-input-4>", line 1, in <module>
        '{:_.20}'.format('453455')
        ~~~~~~~~~~~~~~~~^^^^^^^^^^
    ValueError: Cannot specify '_' with 's'.
    
  6. skirpichev commented on Jul 14, 2025

    @skirpichev
    Member

    Ah, I thought my patch was backported: #132155

  7. added
    3.13only security fixes
    and removed on Jul 14, 2025
  8. skirpichev commented on Jul 14, 2025

    @skirpichev
    Member

    Backport for 3.13: #136649

    3.12 is security-only, so we can't do anything here.

  9. self-assigned this
    on Jul 14, 2025
  10. StanFromIreland commented on Jul 14, 2025

    @StanFromIreland
    Member

    Backport has been merged, should this not be classified as a duplicate of its issue?

  11. picnixz commented on Jul 14, 2025

    @picnixz
    Member

    Yes, it would be a duplicate now.

  12. removed their assignment
    on Jul 14, 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.13only security fixesextension-modulesC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions