Is there a way to round the number 4.25 to 4.3 instead of 4.2 in R?
x <- sprintf("%-8.1f",4.25) # rounds it to 4.2
Is there a way to round the number 4.25 to 4.3 instead of 4.2 in R?
x <- sprintf("%-8.1f",4.25) # rounds it to 4.2
If you like you can write your own "round
" function like below
fround <- function(x, d = 0) {
x.abs <- abs(x)
s <- sign(x)
u <- trunc(x.abs * 10^d)
s * (u + ifelse(x.abs * 10^d - u >= 0.5, 1, 0)) / 10^d
}
such that
> fround(4.25, 2)
[1] 4.25
> fround(4.25, 1)
[1] 4.3
> fround(4.25, 0)
[1] 4
> fround(-4.25, 2)
[1] -4.25
> fround(-4.25, 1)
[1] -4.3
> fround(-4.25, 0)
[1] -4