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

后端 未结 11 1963
执笔经年
执笔经年 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条回答
  •  猫巷女王i
    2020-11-27 13:03

    I tried this without using any external library or cryptic features and it works!

    Hope it helps someone.

    ceil <- function(val, multiple){
      div = val/multiple
      int_div = as.integer(div)
      return (int_div * multiple + ceiling(div - int_div) * multiple)
    }
    
    > ceil(2.1, 2.2)
    [1] 2.2
    > ceil(3, 2.2)
    [1] 4.4
    > ceil(5, 10)
    [1] 10
    > ceil(0, 10)
    [1] 0
    

提交回复
热议问题