Class: Float
Instance Method Summary collapse
-
#ceil_to(n) ⇒ Object
Rounds Float up to
ndecimal places. -
#floor_to(n) ⇒ Object
Rounds Float down to
ndecimal places. -
#round_to(n) ⇒ Object
Rounds Float to
ndecimal places. -
#to_percent(n = 0) ⇒ Object
Converts Float to percent with
ndecimal places.
Instance Method Details
#ceil_to(n) ⇒ Object
Rounds Float up to n decimal places.
8 9 10 |
# File 'lib/ruby-util/float.rb', line 8 def ceil_to(n) (self * 10 ** n).ceil.to_f / 10 ** n end |
#floor_to(n) ⇒ Object
Rounds Float down to n decimal places.
13 14 15 |
# File 'lib/ruby-util/float.rb', line 13 def floor_to(n) (self * 10 ** n).floor.to_f / 10 ** n end |
#round_to(n) ⇒ Object
Rounds Float to n decimal places.
3 4 5 |
# File 'lib/ruby-util/float.rb', line 3 def round_to(n) (self * 10 ** n).round.to_f / 10 ** n end |
#to_percent(n = 0) ⇒ Object
Converts Float to percent with n decimal places.
18 19 20 |
# File 'lib/ruby-util/float.rb', line 18 def to_percent(n = 0) n > 0 ? (self * 100).round_to(n) : (self * 100).round end |