Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
return int(math.log10(-n)) + 2
return len(str(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.