Finding the number of digits of a given integer.

by Rayreware   Last Updated September 12, 2019 04:20 AM

I know that a way to do this is by using log to the base 10. Or more specifically;

$$n=\lfloor\log_{10}x\rfloor + 1\tag{1}\label{1}$$

Where "$\lfloor{z}\rfloor$", rounds the value of $z$. This works really well, but when it comes to values like $x=9999$, we get $n=5$ when using the standard Eq. 1. This is because $9999\approx10000$ and $\lfloor\log_{10}{10000}\rfloor+1=5$. So is there an formula which can take in any value of $x$ and give the number of digits?

Tags : logarithms


Answers 3


This equation should work for all integer $x$. If $9999$ were to be rounded to $10000$, it is a problem with rounding, not the function itself.

Saketh Malyala
Saketh Malyala
September 12, 2019 04:14 AM

Actually, your formula does work also for $x = 9999$. This is because "$\lfloor z \rfloor$" rounds the value of $z$ down. It's called the floor function, so it basically just removes any fractional part of non-negative numbers. With $x = 9999$, this means that $3 \lt \log_{10}x \lt 4$, so $\lfloor \log_{10}(9999) \rfloor + 1 = 4$, as expected.

John Omielan
John Omielan
September 12, 2019 04:15 AM

$\lfloor z\rfloor$ does not round. It is the floor function which returns the greatest integer that does not exceed $z$. The formula is correct for all positive $x$.

pre-kidney
pre-kidney
September 12, 2019 04:15 AM

Related Questions



Log Power Rule (Substituting a 2^X)

Updated October 02, 2018 08:20 AM

Solving for T in an exponential equation

Updated September 23, 2017 11:20 AM