Consider that a four-digit number x is somewhere between 1000 <= x < 10000
. Taking the log base 10 of all three components gives 3.000 <= log(x, 10) < 4.000
. Taking the floor (or int) of each component and adding one gives 4 <= int(log(x, 10))+1 <= 4
.
Ignoring round-off error, this gives you the number of digits in x.