0

I'm trying to find how to return the number of digits to the left of a decimal point in R. For example,

114.10348239857239579 answer: 3 35.1343 answer:2 9.109 answer:1

thank you!

Stacy
  • 87
  • 4

2 Answers2

2

One way to do this would be to cast the number as an integer, cast it again as character and count the number of characters:

x <- c(114.10348239857239579, 35.1343, 9.109)
nchar(as.character(as.integer(x)))

The result is

[1] 3 2 1
1

I'm not sure I understand your question but it might help.

    my_func <- function(x){
      a <- floor(log10(x)) + 1
      return(a)
    }
Park
  • 14,771
  • 6
  • 10
  • 29