Skip to content

Instantly share code, notes, and snippets.

@skeptycal
Forked from lelandbatey/pprint_number.py
Created January 16, 2021 07:46
Show Gist options
  • Save skeptycal/7871dd32a0000d3c8ff4f4de8b81057d to your computer and use it in GitHub Desktop.
Save skeptycal/7871dd32a0000d3c8ff4f4de8b81057d to your computer and use it in GitHub Desktop.
Pretty print large base 10 numbers and base 2 numbers
# Take a very large number and pretty print it in triplets of 3 digits, each triplet separated by a space.
def pnum_spc(n): print(' '.join([''.join(list(str(n))[::-1][i:i+3]) for i in range(0, len(str(n)), 3)][::-1]))
# >>> pnum_spc(32 ** 13)
# 36 893 488 147 419 103 232
# Print numbers as 32-bit binary numbers w/ spaces giving 4-bit words
def pbin_spc(n): print(' '.join([''.join(list(f'{n:032b}')[::-1][i:i+4][::-1]) for i in range(0, len(f'{n:032b}'), 4)][::-1]))
# >>> "{0:032b}".format(1234)
# '00000000000000000000010011010010'
# >>> pbin_spc(1234)
# 0000 0000 0000 0000 0000 0100 1101 0010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment