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?

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.

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.

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

- ServerfaultXchanger
- SuperuserXchanger
- UbuntuXchanger
- WebappsXchanger
- WebmastersXchanger
- ProgrammersXchanger
- DbaXchanger
- DrupalXchanger
- WordpressXchanger
- MagentoXchanger
- JoomlaXchanger
- AndroidXchanger
- AppleXchanger
- GameXchanger
- GamingXchanger
- BlenderXchanger
- UxXchanger
- CookingXchanger
- PhotoXchanger
- StatsXchanger
- MathXchanger
- DiyXchanger
- GisXchanger
- TexXchanger
- MetaXchanger
- ElectronicsXchanger
- StackoverflowXchanger
- BitcoinXchanger
- EthereumXcanger