3

Where x is any integer, I have a value about x.1..x.4. I want to round it to x.5, and if it is above x.5, say x.7 or x.8, then it should round to (x+1).0. How is that possible? Is there a function in ruby?

sawa
  • 165,429
  • 45
  • 277
  • 381
Aravind
  • 1,391
  • 1
  • 16
  • 41

1 Answers1

10

use (v*2).ceil.to_f / 2 where v is your variable.

sawa
  • 165,429
  • 45
  • 277
  • 381
mrd abd
  • 828
  • 10
  • 19
  • 6
    (v*2).ceil.to_f / 2 => (v*2).ceil / 2.0 – CodeGroover Oct 05 '13 at 13:29
  • 2
    @CodeGroover Whether to adopt your idea is up to mrd abd. If mrd abd wants to adopt that, then mrd abd will do so. Do not change the code in other peoples answer (other than for trivial things like formatting, removing extra part, etc.). – sawa Oct 05 '13 at 13:37