Sha256: 5c27d9214c0917454a8cb92e72bd7cb6e9771f184814f9c11bef6b3fd93efcaa

Contents?: true

Size: 723 Bytes

Versions: 3

Compression:

Stored size: 723 Bytes

Contents

require 'facet/float/round_off'

class Float
  # Rounds to the nearest _n_th degree.
  #
  #   require 'facet/float/round_to'
  #
  #   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
  #
  def round_to( n ) #n=1
    return self if n == 0
    (self * (1.0 / n)).round_off.to_f / (1.0 / n)
  end
end

class Numeric
  # To properly support Float's rounding methods,
  # Numeric must also be augmented.
  def round_to(*args)
    self.to_f.round_to(*args)
  end  
end

class Integer
  # To properly support Float's rounding methods,
  # Integer must also be augmented.
  def round_to(*args)
    self.to_f.round_to(*args)
  end  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-0.7.0 lib/facet/float/round_to.rb
facets-0.7.1 lib/facet/float/round_to.rb
facets-0.7.2 lib/facet/float/round_to.rb