# Is it possible to get the number of digits mathematically?

I was wondering if it was possible to, say, have function $f$ that would return the number of digits in any given positive integer. I tried using some sort of a summation, but that failed quite miserably.

Using piecewise equations, it might be constructed something like this: $$f(x)=\begin{cases}1 & \text{ if } x=1 \\2 & \text{ if } x=52 \\3 & \text{ if } x=230 \\4 & \text{ if } x=5023\\\vdots&\text{ }\vdots\end{cases}$$ This is, however, not what I want. Instead, I would like, if possible, some mathematical way of doing this.

• Try $d(x)=1 + \lfloor \log_{10} x \rfloor$. – mjqxxxx Dec 2 '14 at 22:28
• d(2) = 1.3 so I have 1.3 digits? – MathApprentice Dec 2 '14 at 22:31
• @MathApprentice You didn't take the floor first: $\log_{10}(2)\approx .3$. So, you take the floor of that and get $0$. So, $d(2)=1+0=1$. – Joe Johnson 126 Dec 2 '14 at 22:43
• You may be interested in my answer to this related question: math.stackexchange.com/questions/795412/… – MPW Dec 2 '14 at 22:45
• @MPW Thanks, very interesting. – Conor O'Brien Dec 2 '14 at 22:48

$$f(x) = 1+\lfloor \log_{10}(x)\rfloor$$ More generally, $$f(x) = 1+\lfloor \log_{b}(x)\rfloor$$ returns the number of digits of the number in base $b$.

• so, $1 + log_{10} (2) = 1.3$ so I have 1.3 digits? – MathApprentice Dec 2 '14 at 22:30
• $f(x) = 1+\lfloor \log_{b}(x)\rfloor$, $f(n)=-1$, if $0<n<.1$ – Conor O'Brien Dec 2 '14 at 22:32
• @MathApprentice Don't you see the floor function? – Adhvaitha Dec 2 '14 at 22:43
• Oh that's what the half-brackets mean. Okay. – MathApprentice Dec 2 '14 at 22:47

Hint: The integer part of the logarithm in base 10 is the function you look for.

• Not quite. It's one more than that. – MPW Dec 2 '14 at 22:48
• Of course, I was pointing out which function the OP would want to use to find the solution. Notice the difference between "the solution is" and "you need to look for". – rewritten Dec 2 '14 at 22:51