MCPcopy Index your code
hub / github.com/python/cpython / _insert_thousands_sep

Function _insert_thousands_sep

Lib/_pydecimal.py:6292–6327  ·  view source on GitHub ↗

Insert thousands separators into a digit string. spec is a dictionary whose keys should include 'thousands_sep' and 'grouping'; typically it's the result of parsing the format specifier using _parse_format_specifier. The min_width keyword argument gives the minimum length of the

(digits, spec, min_width=1)

Source from the content-addressed store, hash-verified

6290 raise ValueError('unrecognised format for grouping')
6291
6292def _insert_thousands_sep(digits, spec, min_width=1):
6293 """Insert thousands separators into a digit string.
6294
6295 spec is a dictionary whose keys should include 'thousands_sep' and
6296 'grouping'; typically it's the result of parsing the format
6297 specifier using _parse_format_specifier.
6298
6299 The min_width keyword argument gives the minimum length of the
6300 result, which will be padded on the left with zeros if necessary.
6301
6302 If necessary, the zero padding adds an extra '0' on the left to
6303 avoid a leading thousands separator. For example, inserting
6304 commas every three digits in '123456', with min_width=8, gives
6305 '0,123,456', even though that has length 9.
6306
6307 """
6308
6309 sep = spec['thousands_sep']
6310 grouping = spec['grouping']
6311
6312 groups = []
6313 for l in _group_lengths(grouping):
6314 if l <= 0:
6315 raise ValueError("group length should be positive")
6316 # max(..., 1) forces at least 1 digit to the left of a separator
6317 l = min(max(len(digits), min_width, 1), l)
6318 groups.append('0'*(l - len(digits)) + digits[-l:])
6319 digits = digits[:-l]
6320 min_width -= l
6321 if not digits and min_width <= 0:
6322 break
6323 min_width -= len(sep)
6324 else:
6325 l = max(len(digits), min_width, 1)
6326 groups.append('0'*(l - len(digits)) + digits[-l:])
6327 return sep.join(reversed(groups))
6328
6329def _format_sign(is_negative, spec):
6330 """Determine sign character."""

Callers 1

_format_numberFunction · 0.85

Calls 3

_group_lengthsFunction · 0.85
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…