Round up from .5

前端 未结 7 691
一个人的身影
一个人的身影 2020-11-22 11:41

Yes I know why we always round to the nearest even number if we are in the exact middle (i.e. 2.5 becomes 2) of two numbers. But when I want to evaluate data for some peopl

7条回答
  •  有刺的猬
    2020-11-22 11:50

    This mimics the rounding away from zero at .5:

    round_2 <- function(x, digits = 0) {
      x = x + abs(x) * sign(x) * .Machine$double.eps
      round(x, digits = digits)
    }
    
    round_2(.5 + -2:4)
    
    -2 -1  1  2  3  4  5
    

提交回复
热议问题