# Created by NJG on Sat Aug 27 2011 # # Determine the number of significant digits # # Distributed as http://www.perfdynamics.com/Tools/siground.txt # under MIT lincense http://en.wikipedia.org/wiki/MIT_License sf <- function(nstr) { if(grepl(",",nstr,fixed=TRUE)) { # toss any commas nstr <- gsub(",","", nstr, fixed=TRUE) } nnum <- as.numeric(nstr) if(nnum < 1) { # decimal fraction rdigs <- strsplit(nstr, ".",fixed=TRUE) rlength <- nchar(unlist(rdigs)[2]) fstpos <- ceiling(abs(log10(nnum))) sigfigs <- (rlength + 1) - fstpos } if(nnum >= 1) { if(grepl(".",nstr,fixed=TRUE)) { # decimal nodecipt <- sub(".","", nstr, fixed=TRUE) sigfigs <- nchar(nodecipt) } else { # integer deciadd <- as.numeric(paste(".", nstr, sep="")) rdigs <- strsplit(as.character(deciadd), ".", fixed=TRUE) sigfigs <- nchar(unlist(rdigs)[2]) } } return(sigfigs) } # Example uage: # > sf("1,239.4500") # [1] 8