On Sat, Jul 07, 2012 at 11:52:35AM +0300, 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?
Hi. Try the following. getDigits <- function(x) { out <- format.info(x, digits=10) stopifnot(out[3] == 0) out[2] } The function format.info() rounds the input number to "digits" significant digits and then outputs the width of the field for printing, the number of digits after the decimal dot and some information on the exponent (out[3] == 0, if exponent is not used). So, the required number of digits in the fractional part is out[2]. getDigits(3.123456) [1] 6 Hope this helps. Petr Savicky. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.