Skip to content

gh-130664: support '_' (just as ',') in Decimal's formatting - #132155

Merged
ambv merged 2 commits into
python:mainfrom
skirpichev:decimal-underscore-sep/130664
Apr 15, 2025
Merged

gh-130664: support '_' (just as ',') in Decimal's formatting#132155
ambv merged 2 commits into
python:mainfrom
skirpichev:decimal-underscore-sep/130664

Conversation

@skirpichev

@skirpichevskirpichev commented Apr 6, 2025

Copy link
Copy Markdown
Member
>>> 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%'

```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%'
```
@python-cla-bot

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

(',%', '123.456789', '12,345.6789%'),
(',e', '123456', '1.23456e+5'),
(',E', '123456', '1.23456E+5'),
# ... with '_' instead

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not just use the existing tests with a for loop?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you meant? There is no existing test with '_' separator.

@StanFromIrelandStanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not need a documentation update somewhere too?

Comment threadMisc/NEWS.d/next/Library/2025-04-06-14-34-29.gh-issue-130664.JF2r-U.rst Outdated
…F2r-U.rst
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
@skirpichev

Copy link
Copy Markdown
MemberAuthor

Does this not need a documentation update somewhere too?

We don't have documentation for Decimal's formatting, beyond some notes in the Format Specification Mini-Language. There are no even examples of formatted output (few added in #128698).

@ambv
ambv merged commit e10fe81 into python:mainApr 15, 2025
@skirpichev
skirpichev deleted the decimal-underscore-sep/130664 branch April 15, 2025 11:20
@skirpichevskirpichev added the needs backport to 3.13 bugs and security fixes label Jul 14, 2025
@miss-islington-app

Copy link
Copy Markdown

Thanks @skirpichev for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 14, 2025
…ythonGH-132155)
(cherry picked from commit e10fe81)
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
@bedevere-app

Copy link
Copy Markdown

GH-136649 is a backport of this pull request to the 3.13 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.13 bugs and security fixes label Jul 14, 2025
picnixz pushed a commit that referenced this pull request Jul 14, 2025
…H-132155) (#136649)
gh-130664: support '_' (just as ',') in Decimal's formatting (GH-132155)
(cherry picked from commit e10fe81)
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@skirpichev@StanFromIreland@ambv