Class: Float
- Defined in:
- lib/more/facets/typecast.rb,
lib/core/facets/numeric/round.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#round_at(d) ⇒ Object
Rounds to the given decimal position.
-
#round_to(n) ⇒ Object
Rounds to the nearest _n_th degree.
Class Method Details
.cast_from(object) ⇒ Object
157 158 159 160 161 162 |
# File 'lib/more/facets/typecast.rb', line 157 def cast_from(object) return super rescue TypeCastException return object.to_f if object.respond_to? :to_f raise end |
Instance Method Details
#round_at(d) ⇒ Object
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
60 61 62 |
# File 'lib/core/facets/numeric/round.rb', line 60 def round_at( d ) #d=0 (self * (10.0 ** d)).round_off.to_f / (10.0 ** d) end |
#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
73 74 75 76 |
# File 'lib/core/facets/numeric/round.rb', line 73 def round_to( n ) #n=1 return self if n == 0 (self * (1.0 / n)).round_off.to_f / (1.0 / n) end |