[R] How many digits are there in left of dot of 0.0001 ?

JS Huang js.huang at protective.com
Thu Feb 26 04:31:00 CET 2015


Hi,

  To get the number of digits to the right of decimal point: 
nchar(format(a,scientific=FALSE))-(trunc(log10(max(1,trunc(abs(a)))))+1) -1.  

  The part (trunc(log10(max(1,trunc(abs(a)))))+1) is the number of digits to
the left of decimal.  At the end, subtract 1 for the decimal point.

  Negative number needs more work.

> options(digits=10)
> a <- 0.0001
> nchar(format(a,scientific=FALSE))-(trunc(log10(max(1,trunc(abs(a)))))+1)
> -1
[1] 4
> a <- 999.123456
> nchar(format(a,scientific=FALSE))-(trunc(log10(max(1,trunc(abs(a)))))+1)
> -1
[1] 6



--
View this message in context: http://r.789695.n4.nabble.com/How-many-digits-are-there-in-left-of-dot-of-0-0001-tp4703842p4703849.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list