【问题标题】:R change decimal place without round offR 更改小数位不四舍五入
【发布时间】:2014-07-07 01:24:13
【问题描述】:
> signif(1.89,digits=2)
[1] 1.9

我想要 1.8

【问题讨论】:

  • floor(1.89*10)/10 也许?
  • 这也是我能想到的最好的了。

标签: r dataframe decimalformat


【解决方案1】:

这有点笨拙,但它可以工作并保持所有数字:

x <- 1.829380

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

结果:

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

【讨论】:

    【解决方案2】:
    > 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"
    

    试试this 文章的这个变体,这样您就可以获得更多示例。

    【讨论】:

      猜你喜欢
      • 2011-03-09
      • 2020-06-24
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多