Created
August 31, 2024 12:07
-
-
Save ZCG-coder/481045efb31726a07698886d54845ed8 to your computer and use it in GitHub Desktop.
A tool to pretty-print out a number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import Union | |
| def separate_number(number: str, sep: str = ""): | |
| result = number[::-1] | |
| insert_idx = [] | |
| times = 0 | |
| for idx in range(len(number)): | |
| if idx % 3 == 0: | |
| insert_idx.append(idx + times) | |
| times += 1 | |
| for i in insert_idx: | |
| result = result[:i] + sep + result[i:] | |
| return result[::-1][:-1] # Return reversed result, with final separator removed | |
| def print_number(number: Union[float, int] = 0, sep: str = ""): | |
| if len(sep) > 1: | |
| print("INVALID SEPARATOR") | |
| result = str(number) | |
| if sep: | |
| result = separate_number(result, sep) | |
| return result | |
| print(print_number(4044400, sep=",")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment