summaryrefslogtreecommitdiff
path: root/string_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'string_utils.py')
-rw-r--r--string_utils.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/string_utils.py b/string_utils.py
index b586ae1..83575ff 100644
--- a/string_utils.py
+++ b/string_utils.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
+from itertools import zip_longest
import json
import random
import re
@@ -220,6 +221,33 @@ def strip_escape_sequences(in_str: str) -> str:
return in_str
+def add_thousands_separator(in_str: str, *, separator_char = ',', places = 3) -> str:
+ if isinstance(in_str, int):
+ in_str = f'{in_str}'
+
+ if is_number(in_str):
+ return _add_thousands_separator(
+ in_str,
+ separator_char = separator_char,
+ places = places
+ )
+ raise ValueError(in_str)
+
+
+def _add_thousands_separator(in_str: str, *, separator_char = ',', places = 3) -> str:
+ decimal_part = ""
+ if '.' in in_str:
+ (in_str, decimal_part) = in_str.split('.')
+ tmp = [iter(in_str[::-1])] * places
+ ret = separator_char.join(
+ "".join(x) for x in zip_longest(*tmp, fillvalue=""))[::-1]
+ if len(decimal_part) > 0:
+ ret += '.'
+ ret += decimal_part
+ return ret
+
+
+
# Full url example:
# scheme://username:[email protected]:8042/folder/subfolder/file.extension?param=value&param2=value2#hash
def is_url(in_str: Any, allowed_schemes: Optional[List[str]] = None) -> bool: