Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active January 31, 2017 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMCDragonkai/34d9558828fd1b6aaedd8e89fcd30913 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/34d9558828fd1b6aaedd8e89fcd30913 to your computer and use it in GitHub Desktop.
Get the Digit Length of a Number #python
#!/usr/bin/env python3
import math
def digit_length(n):
# outside of these ranges, we get floating point errors
if n >= -999999999999997 and n <= 999999999999997:
if n > 0:
return int(math.log10(n)) + 1
elif n == 0:
return 1
else:
return int(math.log10(-n)) + 2
else:
return len(str(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment