# [R] number of decimal places in a number?

(Ted Harding) Ted.Harding at wlandres.net
Sat Jul 7 12:04:32 CEST 2012

```On 07-Jul-2012 08:52:35 Martin Ivanov wrote:
>  Dear R users,
>
> I need a function that gets a number and returns its number of
> actual decimal places.
> For example f(3.14) should return 2, f(3.142) should return 3,
> f(3.1400) should also return 2 and so on. Is such function already
> available in R? If not, could you give me a hint how to achieve that?
>

I'm not aware of such a function in R. In any case, it will be
a tricky question to solve in full generality, since R stores
numbers internally in a binary representation and the exact
conversion of this representation to a decimal number may not
match the exact value of the decimal representation of the
original number.

In particular, a number entered as a decimal representation
from the keyboard, or read as such from a text file, may not
be exactly matched by the internal representation in R.

However, that said, the following function definition seems to
do what you are asking for, for cases such as you list:

f<-function(x) {min(which( x*10^(0:20)==floor(x*10^(0:20)) )) - 1}

f(3.14)
# [1] 2
f(3.142)
# [1] 3
f(3.1400)
# [1] 2

Note, however:

f(123456.123456789)
# [1] 9

f(123456789.123456789)
#[1] 7

(a consequence of the fact that R does not have enough binary
digits in its binary representation to accommodate the precision
in all the decimal digits of 123456789.123456789 -- not that it
can do that exactly anyway in binary, no matter how many binary

Similarly:

f(pi)
# [1] 15
f(sqrt(2))
# [1] 16

which is a consequence of the fact that 2 < pi < 4, while
1 < sqrt(2) < 2, so the binary representation of pi needs
1 more binary digit for its integer part than sqrt(2) does,
which it therefore has to "steal" from the fractional part.

Hoping this helps,
Ted.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 07-Jul-2012  Time: 11:04:26
This message was sent by XFMail

```