Skip to content

Commit 8e54f7a

Browse files
skirpichevmiss-islington
authored andcommitted
pythongh-87790: support thousands separators for formatting fractional part of Fraction (pythonGH-132204)
(cherry picked from commit 3e849d7) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
1 parent b2a2f03 commit 8e54f7a

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-9]+)?
172172
(?P<thousands_sep>[,_])?
173-
(?:\.(?P<precision>[0-9]+))?
173+
(?:\.
174+
(?=[,_0-9]) # lookahead for digit or separator
175+
(?P<precision>[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
@@ -555,6 +560,9 @@ def _format_float_style(self, match):
555560
if trim_zeros:
556561
frac_part = frac_part.rstrip("0")
557562
separator = "" if trim_point and not frac_part else "."
563+
if frac_sep:
564+
frac_part = frac_sep.join(frac_part[pos:pos + 3]
565+
for pos in range(0, len(frac_part), 3))
558566
trailing = separator + frac_part + suffix
559567

560568
# Do zero padding if required.

Lib/test/test_fractions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,8 @@ def test_format_e_presentation_type(self):
13221322
# Thousands separators
13231323
(F('1234567.123456'), ',.5e', '1.23457e+06'),
13241324
(F('123.123456'), '012_.2e', '0_001.23e+02'),
1325+
# Thousands separators for fractional part (or for integral too)
1326+
(F('1234567.123456'), '.5_e', '1.234_57e+06'),
13251327
# z flag is legal, but never makes a difference to the output
13261328
(F(-1, 7**100), 'z.6e', '-3.091690e-85'),
13271329
]
@@ -1447,6 +1449,12 @@ def test_format_f_presentation_type(self):
14471449
(F('1234567'), ',.2f', '1,234,567.00'),
14481450
(F('12345678'), ',.2f', '12,345,678.00'),
14491451
(F('12345678'), ',f', '12,345,678.000000'),
1452+
# Thousands separators for fractional part (or for integral too)
1453+
(F('123456.789123123'), '._f', '123456.789_123'),
1454+
(F('123456.789123123'), '.7_f', '123456.789_123_1'),
1455+
(F('123456.789123123'), '.9_f', '123456.789_123_123'),
1456+
(F('123456.789123123'), '.,f', '123456.789,123'),
1457+
(F('123456.789123123'), '_.,f', '123_456.789,123'),
14501458
# Underscore as thousands separator
14511459
(F(2, 3), '_.2f', '0.67'),
14521460
(F(2, 3), '_.7f', '0.6666667'),
@@ -1620,6 +1628,11 @@ def test_invalid_formats(self):
16201628
'.f',
16211629
'.g',
16221630
'.%',
1631+
# Thousands separators before precision
1632+
'._6e',
1633+
'._6f',
1634+
'._6g',
1635+
'._6%',
16231636
# Z instead of z for negative zero suppression
16241637
'Z.2f'
16251638
# 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy