0

I would like to have the output of my if statement have two digits of precision.

I have tried using the following with no success:

options(digits=)

format(round())

formatC()

Code

x <- 30
y <- 120.00

if(x %% 5 == 0 & y >= x + 0.50){
  y - x - 0.50
}else{
  y
}
#> [1] 89.5

Created on 2020-08-23 by the reprex package (v0.3.0)

I would like the output for this, and future iterations with differing x and y values, to have two digits of precision 89.50.

Eric
  • 2,699
  • 5
  • 17

1 Answers1

1

One way would be to use nsmall argument in format.

x <- 30
y <- 120.00

if(x %% 5 == 0 & y >= x + 0.50){
  z <- y - x - 0.50
}else{
  z <- y
}

format(z, nsmall = 2)
#[1] "89.50"

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213