2

How rounding up starting at .6 (not at .5)?

For example, round(53.51245, 4) will return me 53.5125, but I want 53.5124.

How can I specify a separation number (namely increase the values ​​starting from .6)?

red_quark
  • 971
  • 5
  • 20

1 Answers1

1

I'm not sure if this is a duplicate of the post linked to in the comments (but the post may certainly be relevant). From what I understand OP would like to "round" values up or down if they are >= 0.6 or < 0.6, respectively. (The linked post refers to the number of digits a number should be rounded to, which is a different issue.)

In response to OPs question, here is an option where we define a custom function my.round

my.round <- function(x, digits = 4, val = 0.6) {
    z <- x * 10^digits
    z <- ifelse(signif(z - trunc(z), 1) >= val, trunc(z + 1), trunc(z))
    z / 10^digits
}

Then

x <- 53.51245
my.round(x, 4)
#[1] 53.5124

x <- 53.51246
my.round(x, 4)
#[1] 53.5125 

my.round is vectorised, so we could have done

my.round(c(53.51245, 53.51246, 53.51246789), digits = 4)
#[1] 53.5124 53.5125 53.5125
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68