Class Float
In: lib/core/facets/numeric/round.rb
Parent: Object

Methods

round_at   round_to  

Public Instance methods

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

[Source]

    # File lib/core/facets/numeric/round.rb, line 50
50:   def round_at( d ) #d=0
51:     (self * (10.0 ** d)).round.to_f / (10.0 ** d)
52:   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

[Source]

    # File lib/core/facets/numeric/round.rb, line 63
63:   def round_to( n ) #n=1
64:     return self if n == 0
65:     (self * (1.0 / n)).round.to_f / (1.0 / n)
66:   end

[Validate]