if i have an integer like this:
123456789
I would like to return only 12345 - so trimming the number to a length of 5 digits.
Is this possible without first converting to a string? I cant see any built in function to do this.
import math
def five_digits(number):
ndigits = int(math.log10(number))+1
try:
return number//int(10**(ndigits-5))
except ZeroDivisionError:
return number
print five_digits(10000000)
print five_digits(12345678)
print five_digits(12345)
print five_digits(1234) #don't know what you want to do with this one...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With