根据条件的百分比百分比

时间:2020-07-23 10:45:48

标签: r

是否可以对数字进行舍入(根据条件删除指数)。例子

> paste0(as.character(as.numeric(0.00000044854548)), "%")
[1] "4.4854548e-07%"
> paste0(as.character(as.numeric(0.44854548)), "%")
[1] "0.44854548%"
````

expected output (basically stick to 4 non-zero numbers), if it the number is 0.00000044854548, then "0.0000004485%". If the number is 0.44854548, then  "0.4485%"
````
> paste0(as.character(as.numeric(0.00000044854548)), "%")
[1] "0.0000004485%"
> paste0(as.character(as.numeric(0.44854548)), "%")
[1] "0.4485%"
````

1 个答案:

答案 0 :(得分:0)

您可以使用formatC

paste0(formatC(0.00000044854548, format="fg", digits=4), "%")
# [1] "0.0000004485%"
paste0(formatC(0.44854548, format="fg", digits=4), "%")
# [1] "0.4485%"