A nuance on this question: how to return number of decimal places in R
How can I count the number of digits after the decimal when there are trailing zeros?
Using the function in the accepted answer :
decimalplaces <- function(x) {
if ((x %% 1) != 0) {
nchar(strsplit(sub('0+$', '', as.character(x)), ".", fixed=TRUE)[[1]][[2]])
} else {
return(0)
}
}
The goal is to yield a count that includes zeros
With the existing function it ignores trailing zeros, e.g.:
> decimalplaces(10)
[1] 0
> decimalplaces(10.0)
[1] 0
> decimalplaces(10.00)
[1] 0
Should return 0, 1, and 2 for the examples above.