How to find the length of integer? Like if I have variable A
as below:
A = 1000;
Then it should return 4
as the length of that integer.
How to find the length of integer? Like if I have variable A
as below:
A = 1000;
Then it should return 4
as the length of that integer.
you can do yhis thing by **converting Integer into String ** and you can easily find length of String very easily in python by using len()
function;
A = 10000;
length = len(str(A));
Mathematically it can be done with log10() and proper rounding, but easier with recursion
def num2length(x, len=0):
if x < 10:
return len+1
return num2length(x/10, len+1)
print(num2length(1000))