# Finding the number of digits of a given integer. [duplicate]

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?

## marked as duplicate by Matthew Daly, Servaes, Feng Shao, Daniele Tampieri, Alan MunizSep 13 at 11:09

• It doesn't matter that $\lfloor \log_{10}10000\rfloor = 4$. $\lfloor \log_{10}999 \rfloor = \lfloor 3.9999...\rfloor = 3$. Notice $3 \le 3.9999...... < 4$. So the floor is $3$. It doesn't matter that $3.9999....$ is close to $4$. It is still less than $4$. – fleablood Sep 12 at 4:40
• So you guys are telling me that its not rounding but a floor function? Wasted some time to come up with a formula that doesn't involve floor. Could have been done easily with floor – Rayreware Sep 12 at 8:13
• BTW, if you only have a rounding function, but you need a floor function, then floor(x) = round(x-.5). But if x starts out as an integer, be careful about the rounding. – Teepeemm Sep 12 at 12:58
• Functions aren't magical incantations. If $10^k \le n < 10^{k+1}$ then then integer $n$ will have $k+1$ digits. Thus $k \le \log_{10} n < k+1$. For any $n$ there is a such a unique number $k+1$ so you define the function to be it. If you don't have a floor function, express the concept with what you have. – fleablood Sep 12 at 15:28
• 9 upvotes? Really? I certainly don't understand how voting works on StackExchange sites... – sanyash Sep 12 at 19:48

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 (e.g., see Wikipedia's Floor and ceiling functions article), so it basically just removes any fractional part of non-negative numbers. Also, note the floor function is applied to the result of the logarithm, not the value of $$x$$ itself. In particular, with $$x = 9999$$, you have $$3 \lt \log_{10}x \lt 4$$, so $$\lfloor \log_{10}(9999) \rfloor + 1 = 3 + 1 = 4$$, as expected.

$$\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$$.

• It does round, it just doesn't necessarily round to the closest integer, it rounds to the next integer down. Both are types of rounding. – Acccumulation Sep 12 at 15:38

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.

This works really well, but when it comes to values like $$x=9999$$, we get $$n=5$$ when using the standard Eq. 1.

No, we don't, but I imagine that might depend on your calculator and rounding error. Using Wolfram Alpha,

$$\log_{10} 9999 \approx 3.99996...$$

which floors to $$3$$, plus one gives $$4$$, as expected. Indeed, even taking $$10^{100} - 1$$ (a number of $$100$$ nines) into Wolfram, we see

$$\log_{10}(10^{100} - 1) \approx 99.\underbrace{999 \cdots 999}_{\text{100 nines}}56570551810...$$

for which the formula still gives the expected result.

My assumption for the source of the discrepancy is either:

• A rounding error in your calculator when trying to calculate the logarithm.
• A misunderstanding, either by yourself or the calculator, of what $$\lfloor x \rfloor$$ "means," in the sense $$\lfloor x \rfloor$$ is the greatest integer such that $$\lfloor x \rfloor \le x$$.

It doesn't matter that $$9999 \approx 10000$$. The floor function always rounds down no matter how close it gets.

$$\log_{10} 9999 = 3.9999565683801924896154439559762.....$$ and $$\lfloor 3.9999565683801924896154439559762..... \rfloor = 3$$. It does not equal $$4$$. That is because even though $$\log_{10}9999\approx \log_{10}10000$$ it is still less than $$4$$. And the floor function NEVER rounds up. It always rounds down.

$$\log_{10}{9999}=a\Longleftrightarrow 10^a=9999.$$
$$10^3=1000$$ and $$10^4=10000$$. Therefore, $$a$$ is a number between $$3$$ and $$4$$. Applying the floor function to a number that's strictly less than $$4$$ and strictly greater than $$3$$ will give you $$3$$. Adding $$1$$ to it will give you $$4$$. That's your answer. Logically, there is absolutely nothing wrong with this method.
More precisely, $$\log_{10}{9999}=3.99995656838019248962...$$, which is a number close to $$4$$. Whatever you're using might be rounding $$\log_{10}{9999}$$ up to $$4$$. Then, it computes the floor of $$4$$, which is $$4$$, and adds $$1$$ to it and you get the incorrect answer of $$5$$. That could be one possible explanation why you get $$5$$ instead of $$4$$.