Class: Float

Inherits:
Object show all
Defined in:
lib/core/facets/numeric/round_to.rb

Instance Method Summary collapse

Instance Method Details

#round_to(n) ⇒ Object

Rounds to the nearest _n_th degree.

4.555.round_to(1)     #=> 5.0
4.555.round_to(0.1)   #=> 4.6
4.555.round_to(0.01)  #=> 4.56
4.555.round_to(0)     #=> 4.555

CREDIT: Trans


26
27
28
29
# File 'lib/core/facets/numeric/round_to.rb', line 26

def round_to( n ) #n=1
  return self if n == 0
  (self * (1.0 / n)).round.to_f / (1.0 / n)
end