What is the better way to count the number of digit in a number?

  1. digit_count recursively divide the number by its base (10)
  2. cast it into string and use str_length to count the number of characters
digit_count <- function(number,count=1) {
        if (number < 10) {return(count)}
    return(digit_count(number %/% 10, count=count+1))
}

Definitions

a = function(x) {return(digit_count(x))}
b = function(x) {return(stringr::str_length(as.character(1000)))}

Timing