Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count leading zeros between the decimal point and first nonzero digit

Tags:

regex

r

desctools

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.

like image 520
Annie Avatar asked Feb 22 '16 12:02

Annie


People also ask

How many zeros are there between the decimal point and the first non-zero digit after the decimal point?

The numbers with n zeros between the decimal point and the first nonzero lie in the interval [10−(n+1),10−n). So given a number x, the number of zeros is ⌈−log10x⌉−1. Indeed log10((2/3)30)=−5.28 which gives you the 5 zeros.

What is the use of the zeros between the decimal point and the non-zero digit?

Zeros can be used as (insignificant) place holders to the left of significant digits if the number is a decimal. For example, a mass of 42 g has two significant digits.

What is a leading zero in a decimal?

The zero before a decimal point is known as a leading zero. Have you noticed that sometimes this zero is used in decimal values and sometimes it is not? APA Style has a very simple guideline for leading zeros: If a value has the potential to exceed 1.0, use the leading zero.

Is zero a nonzero digit?

Zero number definition The zero number is not positive number and not negative number. The zero is also a placeholder digit in other numbers (e.g: 40,103, 170).


1 Answers

Using regexpr and its match.length argument

attr(regexpr("(?<=\\.)0+", x, perl = TRUE), "match.length") 
like image 158
David Arenburg Avatar answered Nov 04 '22 11:11

David Arenburg