Skip to content

Commit 77d7597

Browse files
committed
gh-87790: support thousands separators for formatting fractional part of Fraction's
1 parent ce49022 commit 77d7597

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Lib/fractions.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ def _round_to_figures(n, d, figures):
170170
(?P<zeropad>0(?=[0-9]))?
171171
(?P<minimumwidth>0|[1-9][0-9]*)?
172172
(?P<thousands_sep>[,_])?
173-
(?:\.(?P<precision>0|[1-9][0-9]*))?
173+
(?:\.
174+
(?=[,_0-9]) # lookahead for digit or separator
175+
(?P<precision>0|(?!0)[0-9]+)?
176+
(?P<frac_separators>[,_])?
177+
)?
174178
(?P<presentation_type>[eEfFgG%])
175179
""", re.DOTALL | re.VERBOSE).fullmatch
176180

@@ -499,6 +503,7 @@ def _format_float_style(self, match):
499503
minimumwidth = int(match["minimumwidth"] or "0")
500504
thousands_sep = match["thousands_sep"]
501505
precision = int(match["precision"] or "6")
506+
frac_sep = match["frac_separators"] or ""
502507
presentation_type = match["presentation_type"]
503508
trim_zeros = presentation_type in "gG" and not alternate_form
504509
trim_point = not alternate_form
@@ -552,6 +557,9 @@ def _format_float_style(self, match):
552557
if trim_zeros:
553558
frac_part = frac_part.rstrip("0")
554559
separator = "" if trim_point and not frac_part else "."
560+
if frac_sep:
561+
frac_part = frac_sep.join(frac_part[pos:pos + 3]
562+
for pos in range(0, len(frac_part), 3))
555563
trailing = separator + frac_part + suffix
556564

557565
# Do zero padding if required.

Lib/test/test_fractions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,8 @@ def test_format_e_presentation_type(self):
13341334
# Thousands separators
13351335
(F('1234567.123456'), ',.5e', '1.23457e+06'),
13361336
(F('123.123456'), '012_.2e', '0_001.23e+02'),
1337+
# Thousands separators for fractional part (or for integral too)
1338+
(F('1234567.123456'), '.5_e', '1.234_57e+06'),
13371339
# z flag is legal, but never makes a difference to the output
13381340
(F(-1, 7**100), 'z.6e', '-3.091690e-85'),
13391341
]
@@ -1459,6 +1461,12 @@ def test_format_f_presentation_type(self):
14591461
(F('1234567'), ',.2f', '1,234,567.00'),
14601462
(F('12345678'), ',.2f', '12,345,678.00'),
14611463
(F('12345678'), ',f', '12,345,678.000000'),
1464+
# Thousands separators for fractional part (or for integral too)
1465+
(F('123456.789123123'), '._f', '123456.789_123'),
1466+
(F('123456.789123123'), '.7_f', '123456.789_123_1'),
1467+
(F('123456.789123123'), '.9_f', '123456.789_123_123'),
1468+
(F('123456.789123123'), '.,f', '123456.789,123'),
1469+
(F('123456.789123123'), '_.,f', '123_456.789,123'),
14621470
# Underscore as thousands separator
14631471
(F(2, 3), '_.2f', '0.67'),
14641472
(F(2, 3), '_.7f', '0.6666667'),
@@ -1639,6 +1647,11 @@ def test_invalid_formats(self):
16391647
'.f',
16401648
'.g',
16411649
'.%',
1650+
# Thousands separators before precision
1651+
'._6e',
1652+
'._6f',
1653+
'._6g',
1654+
'._6%',
16421655
# Z instead of z for negative zero suppression
16431656
'Z.2f'
16441657
# z flag not supported for general formatting
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Support underscore and comma as thousands separators in the fractional part
2+
for :class:`~fractions.Fraction`'s formatting. Patch by Sergey B Kirpichev.

0 commit comments

Comments
 (0)