[R] rounding up (always)

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Wed Oct 20 23:16:04 CEST 2010


Hello!

I am trying to round the number always up - i.e., whatever the
positive number is, I would like it to round it to the closest 10 that
is higher than this number, the closest 100 that is higher than this
number, etc.

For example:
x<-3241.388

signif(x,1) rounds to the closest thousand, i.e., to 3,000, but I'd
like to get 4,000 instead.
signif(x,2) rounds to the closest hundred, i.e., to 3,200, but I'd
like to get 3,300 instead.
signif(x,3) rounds to the closest ten, i.e., to 3,240, but I'd like to
get 3,250 instead.

Of course, I could do:
floor(signif(x,1)+1000)
floor(signif(x,2)+100)
floor(signif(x,3)+10)

But it's very manual - because in the problem I am facing the numbers
sometimes have to be rounded to a 1000, sometimes to a 100, etc.


Thanks a lot for any hints!

-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com



More information about the R-help mailing list