rregexdesctools

Count leading zeros between the decimal point and first nonzero digit


Suppose if we have a number 1.000633, I want to count number of zeros after the decimal point until first nonzero digit in the fraction, the answer should be 3. For 0.002 the answer should be 2.

There is no such function in R that could help. I have explored at Ndec function in package DescTools but it does not do the job.


Solution

  • Using regexpr and its match.length argument

    attr(regexpr("(?<=\\.)0+", x, perl = TRUE), "match.length")