Class: Float

Inherits:
Object show all
Defined in:
lib/ruby_smart/support/core_ext/ruby/float.rb,
lib/ruby_smart/support/core_ext/ruby/float.rb

Instance Method Summary collapse

Instance Method Details

#round_down(exp = 0) ⇒ Float

returns a new float and rounds down

Examples:

nb = 45.5678
nb.round_down(2)
> 45.56

Parameters:

  • exp (Integer) (defaults to: 0)
    • amount of decimals

Returns:

  • (Float)

    rounded number



14
15
16
17
# File 'lib/ruby_smart/support/core_ext/ruby/float.rb', line 14

def round_down(exp = 0)
  multiplier = 10 ** exp
  ((self * multiplier).floor).to_f/multiplier.to_f
end

#round_up(exp = 0) ⇒ Float

returns a new float and rounds up

Examples:

nb = 45.5678
nb.round_up(2)
> 45.57

Parameters:

  • exp (Integer) (defaults to: 0)
    • amount of decimals

Returns:

  • (Float)

    rounded number



32
33
34
35
# File 'lib/ruby_smart/support/core_ext/ruby/float.rb', line 32

def round_up(exp = 0)
  multiplier = 10 ** exp
  ((self * multiplier).ceil).to_f/multiplier.to_f
end