MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / currency

Function currency

tools/python-3.11.9-amd64/Lib/locale.py:265–310  ·  view source on GitHub ↗

Formats val according to the currency settings in the current locale.

(val, symbol=True, grouping=False, international=False)

Source from the content-addressed store, hash-verified

263 return _format(percent, value, grouping, monetary, *additional)
264
265def currency(val, symbol=True, grouping=False, international=False):
266 """Formats val according to the currency settings
267 in the current locale."""
268 conv = localeconv()
269
270 # check for illegal values
271 digits = conv[international and 'int_frac_digits' or 'frac_digits']
272 if digits == 127:
273 raise ValueError("Currency formatting is not possible using "
274 "the 'C' locale.")
275
276 s = _localize(f'{abs(val):.{digits}f}', grouping, monetary=True)
277 # '<' and '>' are markers if the sign must be inserted between symbol and value
278 s = '<' + s + '>'
279
280 if symbol:
281 smb = conv[international and 'int_curr_symbol' or 'currency_symbol']
282 precedes = conv[val<0 and 'n_cs_precedes' or 'p_cs_precedes']
283 separated = conv[val<0 and 'n_sep_by_space' or 'p_sep_by_space']
284
285 if precedes:
286 s = smb + (separated and ' ' or '') + s
287 else:
288 if international and smb[-1] == ' ':
289 smb = smb[:-1]
290 s = s + (separated and ' ' or '') + smb
291
292 sign_pos = conv[val<0 and 'n_sign_posn' or 'p_sign_posn']
293 sign = conv[val<0 and 'negative_sign' or 'positive_sign']
294
295 if sign_pos == 0:
296 s = '(' + s + ')'
297 elif sign_pos == 1:
298 s = sign + s
299 elif sign_pos == 2:
300 s = s + sign
301 elif sign_pos == 3:
302 s = s.replace('<', sign)
303 elif sign_pos == 4:
304 s = s.replace('>', sign)
305 else:
306 # the default if nothing specified;
307 # this should be the most fitting sign position
308 s = sign + s
309
310 return s.replace('<', '').replace('>', '')
311
312def str(val):
313 """Convert float to string, taking the locale into account."""

Callers

nothing calls this directly

Calls 4

localeconvFunction · 0.85
_localizeFunction · 0.85
absFunction · 0.70
replaceMethod · 0.45

Tested by

no test coverage detected