I read recently that you can find the number of digits in a number through the formula $\lfloor \log_{10} n \rfloor +1$ What's the logic behind this rather what's the proof?


Suppose that $n$ has $d$ digits; then $10^{d-1}\le n<10^d$, because $10^d$ is the smallest integer with $d+1$ digits. Now take logs base $10$: $\log_{10}10^k=k$, so the inequality becomes $d-1\le\log_{10}n<d$. If you now take the floor (= integer part) of $\log_{10}n$, throwing away everything to the right of the decimal point, you get $\lfloor\log_{10}n\rfloor=d-1$. Thus, $d=\lfloor\log_{10}n\rfloor+1$. This isn’t quite what you posted, but it’s the integer part of it, and clearly the number of digits must be an integer.

  • $\begingroup$ So, given N=8 for example, how many digits would there be in log(8)? Thank you. $\endgroup$ – NoChance Nov 6 '12 at 23:35
  • $\begingroup$ @Emmad: It’s not about the number of digits in $\log_{10}8$; the result is that $8$ has $\lfloor\log_{10}8\rfloor+1$ digits, which is true, since $\log_{10}8\approx0.90309$. $\endgroup$ – Brian M. Scott Nov 6 '12 at 23:38
  • $\begingroup$ I see now, thanks. $\endgroup$ – NoChance Nov 6 '12 at 23:42
  • $\begingroup$ @Emmad: You’re welcome. $\endgroup$ – Brian M. Scott Nov 6 '12 at 23:43
  • 1
    $\begingroup$ @HassamAbdelillah Because log returns a real number and digits is an integral value. $\endgroup$ – NetMage Sep 18 '19 at 21:26

let's consider the binary base-2 system. Any decimal number (say 8) can be represented as $2^3$. So if you take $log_2(8)$ you get 3, which represents the number of bits to represent 8. Thus, any number between 8 and 16 can be represented with $\lceil log_2(N)\rceil$ bits and so on. You can easily extend this to digits.

  • 2
    $\begingroup$ Your formula doesn’t work when $N$ is a power of $2$. It should be $\lfloor\log_2N\rfloor+1$. $\endgroup$ – Brian M. Scott Nov 6 '12 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.