@@ -1400,7 +1400,7 @@ def from_str(x, prec=0, rnd=round_fast, base=0):
14001400 (?P<zeropad>0(?=0*[1-9]))?
14011401 (?P<width>[0-9]+)?
14021402 (?P<thousands_separators>[,_])?
1403- (?:\.(?P<precision>[0-9]+))?
1403+ (?:\.(?P<precision>[0-9]+)(?P<frac_separators>[,_])? )?
14041404 (?P<rounding>[UDYZN])?
14051405 (?P<type>[aAbeEfFgG%])?
14061406""" , re .DOTALL | re .VERBOSE ).fullmatch
@@ -1447,6 +1447,7 @@ def read_format_spec(format_spec):
14471447 'no_neg_0' : False ,
14481448 'alternate' : False ,
14491449 'thousands_separators' : '' ,
1450+ 'frac_separators' : '' ,
14501451 'width' : - 1 ,
14511452 'precision' : - 1 ,
14521453 'rounding' : round_nearest ,
@@ -1464,6 +1465,8 @@ def read_format_spec(format_spec):
14641465 or format_dict ['thousands_separators' ]
14651466 format_dict ['width' ] = int (match ['width' ] or format_dict ['width' ])
14661467 format_dict ['precision' ] = int (match ['precision' ] or format_dict ['precision' ])
1468+ format_dict ['frac_separators' ] = match ['frac_separators' ] \
1469+ or format_dict ['frac_separators' ]
14671470 rounding_char = match ['rounding' ]
14681471 format_dict ['type' ] = match ['type' ] or format_dict ['type' ]
14691472
@@ -1681,13 +1684,21 @@ def format_digits(num, format_dict, prec):
16811684 or (precision and not strip_last_zero )):
16821685 frac_part = '.' + frac_part
16831686
1687+ sep_range = 3
1688+ frac_sep = format_dict ['frac_separators' ]
1689+ if frac_sep and frac_part :
1690+ frac_part = frac_part [:sep_range + 1 ] + "" .join (frac_sep + frac_part [pos :pos + sep_range ]
1691+ for pos in range (sep_range + 1 ,
1692+ len (frac_part ),
1693+ sep_range ))
16841694 digits = frac_part + exponent
16851695
16861696 sign = '-' if num [0 ] else ''
16871697 if sign != '-' and format_dict ['sign' ] != '-' :
16881698 sign = format_dict ['sign' ]
16891699 if fmt_type == 'f' and format_dict ['no_neg_0' ]:
1690- if int_part == "0" and all (_ in ['0' , '.' ] for _ in digits ):
1700+ if int_part == "0" and all (_ in ['0' , '.' , '_' , ',' ]
1701+ for _ in digits ):
16911702 if format_dict ['sign' ] == '-' :
16921703 sign = ''
16931704 else :
@@ -1697,7 +1708,6 @@ def format_digits(num, format_dict, prec):
16971708 digits += '%'
16981709
16991710 sep = format_dict ['thousands_separators' ]
1700- sep_range = 3
17011711 width = format_dict ['width' ]
17021712 if (int_part and fmt_type in ['%' , 'f' , 'e' , 'g' , '' ]
17031713 and format_dict ['fill_char' ] == '0' and format_dict ['align' ] == '='
0 commit comments