R/utils_digits.R

Defines functions utils_digits

Documented in utils_digits

#' Number of Decimal Places
#'
#' @param x (required, numeric) Default: NULL
#'
#' @return integer
#' @export
#' @autoglobal
#' @examples
#' utils_digits(x = 0.234)
#' @family internal
utils_digits <- function(
    x = NULL
    ){

  out <- 0

  if(abs(x - round(x)) > .Machine$double.eps^0.5){

    #remove terminal zeros
    x <- sub(
      pattern = '0+$',
      replacement = '',
      x = as.character(x)
    )

    #number of decimals
    out <- nchar(
      x = strsplit(
        x = x,
        split = ".",
        fixed = TRUE
      )[[1]][[2]]
    )

  }

  out

}

Try the distantia package in your browser

Any scripts or data that you put into this service are public.

distantia documentation built on April 4, 2025, 5:42 a.m.