Skip to content

Instantly share code, notes, and snippets.

@ZCG-coder
Created August 31, 2024 12:07
Show Gist options
  • Select an option

  • Save ZCG-coder/481045efb31726a07698886d54845ed8 to your computer and use it in GitHub Desktop.

Select an option

Save ZCG-coder/481045efb31726a07698886d54845ed8 to your computer and use it in GitHub Desktop.
A tool to pretty-print out a number
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