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 — `,` works with `Decimal()` against the doc of **Format Specification Mini-Language** · Issue #136636 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed as duplicate
Closed as duplicate
, works with Decimal() against the doc of **Format Specification Mini-Language**#136636
Copy link
Labels
3.13only security fixesonly security fixesextension-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

@hyperkai
hyperkai
opened on Jul 13, 2025
Issue body actions

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. hyperkai
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jul 13, 2025
  2. picnixz commented on Jul 13, 2025

    @picnixz
    picnixz
    on Jul 13, 2025
    Member
    More actions

    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

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

    @skirpichev
    skirpichev
    on Jul 14, 2025
    Member
    More actions

    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.)

  5. skirpichev
    closed this as not plannedon Jul 14, 2025
  6. skirpichev
    added
    invalid
    and removed
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    on Jul 14, 2025
  7. terryjreedy commented on Jul 14, 2025

    @terryjreedy
    terryjreedy
    on Jul 14, 2025
    Member
    More actions

    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'.
    
  8. skirpichev commented on Jul 14, 2025

    @skirpichev
    skirpichev
    on Jul 14, 2025
    Member
    More actions

    Ah, I thought my patch was backported: #132155

  9. skirpichev
    reopened this on Jul 14, 2025
  10. skirpichev
    added
    3.13only security fixesonly security fixes
    and removed
    invalid
    on Jul 14, 2025
  11. skirpichev commented on Jul 14, 2025

    @skirpichev
    skirpichev
    on Jul 14, 2025
    Member
    More actions

    Backport for 3.13: #136649

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

  12. skirpichev
    self-assigned this
    on Jul 14, 2025
  13. StanFromIreland
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Jul 14, 2025
  14. StanFromIreland commented on Jul 14, 2025

    @StanFromIreland
    StanFromIreland
    on Jul 14, 2025
    Member
    More actions

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

  15. picnixz commented on Jul 14, 2025

    @picnixz
    picnixz
    on Jul 14, 2025
    Member
    More actions

    Yes, it would be a duplicate now.

  16. picnixz
    closed this as a duplicateof Undocumented handling of thousands separator for floats and differences wrt Decimal #130664 on Jul 14, 2025
  17. skirpichev
    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 fixesonly security fixesextension-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

    No projects

      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.