0
> signif(1.89,digits=2)
[1] 1.9

I'd like to have 1.8

Jaap
  • 81,064
  • 34
  • 182
  • 193
DemetriusRPaula
  • 377
  • 1
  • 10

2 Answers2

0

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
thelatemail
  • 91,185
  • 12
  • 128
  • 188
0
> 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.

Community
  • 1
  • 1
bumbumpaw
  • 2,522
  • 1
  • 24
  • 54