R/utils.num.decimals.R

Defines functions max_num_decimals num_decimals

Documented in max_num_decimals num_decimals

#' How many significant decimals places are in a number
#'
#' @param x
#' @param decimal.char
#'
#' @return
#' @export
#'
#' @examples
num_decimals <- function(x, decimal.char=".", digits = 4) {
  if(!is.numeric(x)) return(0)
  x <- round(x, digits = digits)
  if (abs(x - round(x)) > .Machine$double.eps^0.5) {
    nchar(strsplit(sub('0+$', '', as.character(x)), decimal.char, fixed=TRUE)[[1]][[2]])
  } else {
    return(0)
  }
}




#' Max number of significant decimal places are in a vector
#'
#' @param x
#' @param decimal.char
#' @param digits
#'
#' @return
#' @export
#'
#' @examples
max_num_decimals <- function(x, decimal.char = ".", digits = 4) {
  if(!is.numeric(x)) return(0)
  max(sapply(x,num_decimals, decimal.char=decimal.char, digits=digits))
}
feranpre/feR documentation built on Nov. 22, 2022, 2:29 a.m.