MCPcopy
hub / github.com/django/django / sanitize_separators

Function sanitize_separators

django/utils/formats.py:276–305  ·  view source on GitHub ↗

Sanitize a value according to the current decimal and thousand separator setting. Used with form field input.

(value)

Source from the content-addressed store, hash-verified

274
275
276def sanitize_separators(value):
277 """
278 Sanitize a value according to the current decimal and
279 thousand separator setting. Used with form field input.
280 """
281 if isinstance(value, str):
282 parts = []
283 decimal_separator = get_format("DECIMAL_SEPARATOR")
284 if decimal_separator in value:
285 value, decimals = value.split(decimal_separator, 1)
286 parts.append(decimals)
287 if settings.USE_THOUSAND_SEPARATOR:
288 thousand_sep = get_format("THOUSAND_SEPARATOR")
289 if (
290 thousand_sep == "."
291 and value.count(".") == 1
292 and len(value.split(".")[-1]) != 3
293 ):
294 # Special case where we suspect a dot meant decimal separator
295 # (see #22171).
296 pass
297 else:
298 for replacement in {
299 thousand_sep,
300 unicodedata.normalize("NFKD", thousand_sep),
301 }:
302 value = value.replace(replacement, "")
303 parts.append(value)
304 value = ".".join(reversed(parts))
305 return value

Callers 1

Calls 6

get_formatFunction · 0.85
splitMethod · 0.45
appendMethod · 0.45
countMethod · 0.45
normalizeMethod · 0.45
joinMethod · 0.45

Tested by 1