gh-87790: support thousands separators for formatting fractional part… · python/cpython@90a5b44 · GitHub
Skip to content

Commit 90a5b44

Browse files
gh-87790: support thousands separators for formatting fractional part of Decimal (#132202)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 0c3e3da commit 90a5b44

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

Lib/_pydecimal.py

Lines changed: 13 additions & 1 deletion

Lib/test/test_decimal.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,15 @@ def test_formatting(self):
10891089
('07_', '1234.56', '1_234.56'),
10901090
('_', '1.23456789', '1.23456789'),
10911091
('_%', '123.456789', '12_345.6789%'),
1092+
# and now for something completely different...
1093+
('.,', '1.23456789', '1.234,567,89'),
1094+
('._', '1.23456789', '1.234_567_89'),
1095+
('.6_f', '12345.23456789', '12345.234_568'),
1096+
(',._%', '123.456789', '12,345.678_9%'),
1097+
(',._e', '123456', '1.234_56e+5'),
1098+
(',.4_e', '123456', '1.234_6e+5'),
1099+
(',.3_e', '123456', '1.235e+5'),
1100+
(',._E', '123456', '1.234_56E+5'),
10921101

10931102
# negative zero: default behavior
10941103
('.1f', '-0', '-0.0'),
@@ -1162,6 +1171,10 @@ def test_formatting(self):
11621171
# bytes format argument
11631172
self.assertRaises(TypeError, Decimal(1).__format__, b'-020')
11641173

1174+
# precision or fractional part separator should follow after dot
1175+
self.assertRaises(ValueError, format, Decimal(1), '.f')
1176+
self.assertRaises(ValueError, format, Decimal(1), '._6f')
1177+
11651178
def test_negative_zero_format_directed_rounding(self):
11661179
with self.decimal.localcontext() as ctx:
11671180
ctx.rounding = ROUND_CEILING
Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)