[Math] How to get the amount of digits after the decimal point

arithmeticdecimal-expansion

How can I easily find the amount of digits after the decimal point?
For example:

0.2 would result 1 (1 digit after the point)
0.09 would result 2 (2 digits after the point)
0.641 would result 3 (3 digits after the point)

I think this is fairly simple, but I just can't get my head around it right now…

Thanks!

Best Answer

The answer depends on just how you store your number.

If you are storing it as a rational number or as a fraction $\frac ab$ in lowest terms, or if you have a routine to convert your number to this format, here is how you can do it.

Find the prime factorization of $b$. Let's say it is

$$b=2^r5^sp^t\ldots$$

If $b$ has a prime factor other than $2$ or $5$, the number of decimal places is infinite. Otherwise, the number of decimal places in $\frac ab$ is

$$\max(r,s)$$

Do you need an explanation of why this works?


If your number $x$ is a float or a real number, find the smallest $t$ such that

$$10^t\cdot x=\lfloor 10^t\cdot x \rfloor$$

(The brackets are the greatest integer function.) Then $t$ is the number of decimal places. If no such $t$ exists, the number of decimal places is infinite.