Last active
January 31, 2017 14:56
-
-
Save CMCDragonkai/34d9558828fd1b6aaedd8e89fcd30913 to your computer and use it in GitHub Desktop.
Get the Digit Length of a Number #python
This file contains 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
#!/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