-2

Consider a number abcde. Here, we have 5 digits in that number. Let, say this abcde = n. Then I want a single mathematical function which tells how much digits are there in n.
They could be 3 or 100.
In programming it's easy but it requires more statements. Is this possible to get a single statement. Generally, I not want this for programming.

Jyrki Lahtonen
  • 133,153

1 Answers1

1

If $n$ is a positive integer, the number of decimal digits of $n$ is $\left\lfloor{\log_{10}(n)}\right\rfloor+1$.

Explanation:

Let $n$ be a positive integer. \begin{align*} \text{Then}\;\;&n\;\text{is a $k$-digit number}\\[4pt] \iff\;&10^{k-1}\le n < 10^{k}\\[4pt] \iff\;&k-1\le \log_{10}(n) < k\\[4pt] \iff\;&k-1=\left\lfloor{\log_{10}(n)}\right\rfloor\\[4pt] \iff\;&k=\left\lfloor{\log_{10}(n)}\right\rfloor+1\\[4pt] \end{align*}

quasi
  • 58,772