@@ -185,8 +185,14 @@ def _format(percent, value, grouping=False, monetary=False, *additional):
185185 formatted = percent % ((value ,) + additional )
186186 else :
187187 formatted = percent % value
188+ if percent [- 1 ] in 'eEfFgGdiu' :
189+ formatted = _localize (formatted , grouping , monetary )
190+ return formatted
191+
192+ # Transform formatted as locale number according to the locale settings
193+ def _localize (formatted , grouping = False , monetary = False ):
188194 # floats and decimal ints need special action!
189- if percent [ - 1 ] in 'eEfFgG' :
195+ if '.' in formatted :
190196 seps = 0
191197 parts = formatted .split ('.' )
192198 if grouping :
@@ -196,7 +202,7 @@ def _format(percent, value, grouping=False, monetary=False, *additional):
196202 formatted = decimal_point .join (parts )
197203 if seps :
198204 formatted = _strip_padding (formatted , seps )
199- elif percent [ - 1 ] in 'diu' :
205+ else :
200206 seps = 0
201207 if grouping :
202208 formatted , seps = _group (formatted , monetary = monetary )
@@ -267,7 +273,7 @@ def currency(val, symbol=True, grouping=False, international=False):
267273 raise ValueError ("Currency formatting is not possible using "
268274 "the 'C' locale." )
269275
270- s = _format ( '%%.%if' % digits , abs (val ), grouping , monetary = True )
276+ s = _localize ( f' { abs (val ):.{ digits }f } ' , grouping , monetary = True )
271277 # '<' and '>' are markers if the sign must be inserted between symbol and value
272278 s = '<' + s + '>'
273279
@@ -323,6 +329,10 @@ def delocalize(string):
323329 string = string .replace (dd , '.' )
324330 return string
325331
332+ def localize (string , grouping = False , monetary = False ):
333+ """Parses a string as locale number according to the locale settings."""
334+ return _localize (string , grouping , monetary )
335+
326336def atof (string , func = float ):
327337 "Parses a string as a float according to the locale settings."
328338 return func (delocalize (string ))
0 commit comments