Sha256: aa317c95143823306f5ebb2f1a9334f958ba74fa492eee17c16d79569929ac7a

Contents?: true

Size: 1.27 KB

Versions: 10

Compression:

Stored size: 1.27 KB

Contents

class Numeric

  # Conceptually, rounding is expected to apply to floating point numbers.
  # However it can actually be applied to pretty much and Numeric object.
  # For example, one could round an Integer to the nearest kilo. So the
  # actual round.rb lib is in the numeric folder, but we'll add this
  # here for convenience.
  #
  # See Float#round_at.

  def round_at(*args)
    to_f.round_at(*args)
  end

  # See Float#round_to.

  def round_to(*args)
    to_f.round_to(*args)
  end

end

class Integer

  # See Float#round_at.

  def round_at(*args)
    to_f.round_at(*args)
  end

  # See Float#round_to.

  def round_to(*args)
    to_f.round_to(*args)
  end

end

class Float

  # Rounds to the given decimal position.
  #
  #   4.555.round_at(0)  #=> 5.0
  #   4.555.round_at(1)  #=> 4.6
  #   4.555.round_at(2)  #=> 4.56
  #   4.555.round_at(3)  #=> 4.555
  #
  # CREDIT: Trans

  def round_at( d ) #d=0
    (self * (10.0 ** d)).round.to_f / (10.0 ** d)
  end

  # 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

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

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/numeric/round.rb
facets-2.8.3 lib/core/facets/numeric/round.rb
facets-2.8.2 lib/core/facets/numeric/round.rb
facets-2.8.1 lib/core/facets/numeric/round.rb
facets-2.8.0 lib/core/facets/numeric/round.rb
facets-2.7.0 lib/core/facets/numeric/round.rb
facets-2.6.0 lib/core/facets/numeric/round.rb
facets-2.5.1 lib/core/facets/numeric/round.rb
facets-2.5.0 lib/core/facets/numeric/round.rb
facets-2.5.2 lib/core/facets/numeric/round.rb