Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/rounding/core_ext/numeric.rb

Instance Method Summary collapse

Instance Method Details

#ceil_to(step, around = 0) ⇒ Object



7
8
9
10
11
# File 'lib/rounding/core_ext/numeric.rb', line 7

def ceil_to(step, around=0)
  whole, remainder = (self - around).divmod(step)
  num_steps = remainder > 0 ? whole + 1 : whole
  num_steps * step + around
end

#floor_to(step, around = 0) ⇒ Object



2
3
4
5
# File 'lib/rounding/core_ext/numeric.rb', line 2

def floor_to(step, around=0)
  whole, _ = (self - around).divmod(step)
  whole * step + around
end

#round_to(step, around = 0) ⇒ Object



13
14
15
16
17
# File 'lib/rounding/core_ext/numeric.rb', line 13

def round_to(step, around=0)
  whole, remainder = (self - around).divmod(step)
  num_steps = remainder*2 >= step ? whole + 1 : whole
  num_steps * step + around
end