How to round up to the nearest 10 (or 100 or X)?

后端 未结 11 1966
执笔经年
执笔经年 2020-11-27 12:20

I am writing a function to plot data. I would like to specify a nice round number for the y-axis max that is greater than the max of the dataset.

Specif

11条回答
  •  不知归路
    2020-11-27 12:54

    Regarding the rounding up to the multiplicity of an arbitrary number, e.g. 10, here is a simple alternative to James's answer.

    It works for any real number being rounded up (from) and any real positive number rounded up to (to):

    > RoundUp <- function(from,to) ceiling(from/to)*to
    

    Example:

    > RoundUp(-11,10)
    [1] -10
    > RoundUp(-0.1,10)
    [1] 0
    > RoundUp(0,10)
    [1] 0
    > RoundUp(8.9,10)
    [1] 10
    > RoundUp(135,10)
    [1] 140
    
    > RoundUp(from=c(1.3,2.4,5.6),to=1.1)  
    [1] 2.2 3.3 6.6
    

提交回复
热议问题