[R] round up/down to nearest hundredth

Petr Savicky savicky at cs.cas.cz
Sun May 13 10:23:18 CEST 2012


On Sat, May 12, 2012 at 01:38:16PM -0700, chuck.01 wrote:
> Hi, 
> How can I round up and down to the nearest hundredth
> 
> example:
> 
> x <- 0.18
> how do I round down to 0.15?
> how do I round down to 0.20?

Hi.

Rounding to 0.15 is not to a nearest hundredth, but to a nearest
multiple of 0.05. Rounding down and up to a nearest multiple
of a unit u can be done as follows

  x <- 0.18
  u <- 0.05
  floor(x/u)*u   # [1] 0.15
  ceiling(x/u)*u # [1] 0.2

Hope this helps.

Petr Savicky.



More information about the R-help mailing list