如何在尾随零的上下文中返回小数位数

时间:2017-06-22 21:15:01

标签: r char type-conversion

关于这个问题的细微差别:how to return number of decimal places in R

当有尾随零时,如何计算小数点后的位数?

使用the accepted answer 中的功能:

decimalplaces <- function(x) {
  if ((x %% 1) != 0) {
    nchar(strsplit(sub('0+$', '', as.character(x)), ".", fixed=TRUE)[[1]][[2]])
  } else {
    return(0)
  }
}

目标是产生一个包含零的计数

使用现有函数,它会忽略尾随零,例如:

 > decimalplaces(10)
[1] 0
> decimalplaces(10.0)
[1] 0
> decimalplaces(10.00)
[1] 0

上述示例应返回0,1和2.

0 个答案:

没有答案
相关问题