MCPcopy Index your code
hub / github.com/RustPython/RustPython / _insert_thousands_sep

Function _insert_thousands_sep

Lib/_pydecimal.py:6289–6324  ·  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

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

Callers 1

_format_numberFunction · 0.85

Calls 7

_group_lengthsFunction · 0.85
minFunction · 0.85
maxFunction · 0.85
lenFunction · 0.85
reversedFunction · 0.85
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected