Skip to content

Commit 1ae18f5

Browse files
committed
gh-130664: support '_' (just as ',') in Decimal's formatting
```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%' ```
1 parent 0dba59e commit 1ae18f5

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

Lib/_pydecimal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6098,7 +6098,7 @@ def _convert_for_comparison(self, other, equality_op=False):
60986098
(?P<alt>\#)?
60996099
(?P<zeropad>0)?
61006100
(?P<minimumwidth>(?!0)\d+)?
6101-
(?P<thousands_sep>,)?
6101+
(?P<thousands_sep>[,_])?
61026102
(?:\.(?P<precision>0|(?!0)\d+))?
61036103
(?P<type>[eEfFgGn%])?
61046104
\Z

Lib/test/test_decimal.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,11 @@ def test_formatting(self):
10821082
(',%', '123.456789', '12,345.6789%'),
10831083
(',e', '123456', '1.23456e+5'),
10841084
(',E', '123456', '1.23456E+5'),
1085+
# ... with '_' instead
1086+
('_', '1234567', '1_234_567'),
1087+
('07_', '1234.56', '1_234.56'),
1088+
('_', '1.23456789', '1.23456789'),
1089+
('_%', '123.456789', '12_345.6789%'),
10851090

10861091
# negative zero: default behavior
10871092
('.1f', '-0', '-0.0'),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Support ``'_'`` digit separator in formatting of the integral part of
2+
:class:`~decimal.Decimal`'s. Patch by Sergey B Kirpichev.

0 commit comments

Comments
 (0)