R change decimal place without round off

好久不见. 提交于 2019-12-01 14:30:18

This is a bit clunky, but it will work and keep everything numeric:

x <- 1.829380

trunc.dec <- function(x,n) {
  floor(x*10^n)/10^n
}

Result:

trunc.dec(x,1)
#[1] 1.8
trunc.dec(x,2)
#[1] 1.82
trunc.dec(x,3)
#[1] 1.829
trunc.dec(x,4)
#[1] 1.8293
bumbumpaw
> format(round(1.20, 2), nsmall = 2)
[1] "1.20"
> format(round(1, 2), nsmall = 2)
[1] "1.00"
> format(round(1.1234, 2), nsmall = 2)
[1] "1.12"

try this variation from this article so you can have more samples.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!