Sha256: 543f46b8dabebb8925a87db94af1f706cf5cb10146e9197a8f836edf9ddc7d90

Contents?: true

Size: 824 Bytes

Versions: 7

Compression:

Stored size: 824 Bytes

Contents

# frozen_string_literal: true

unless Float.method_defined? :round_down
  class Float
    # returns a new float and rounds down
    #
    # @example
    #   nb = 45.5678
    #   nb.round_down(2)
    #   > 45.56
    #
    # @param [Integer] exp - amount of decimals
    # @return [Float] rounded number
    def round_down(exp = 0)
      multiplier = 10 ** exp
      ((self * multiplier).floor).to_f/multiplier.to_f
    end
  end
end

unless Float.method_defined? :round_up
  class Float
    # returns a new float and rounds up
    #
    # @example
    #   nb = 45.5678
    #   nb.round_up(2)
    #   > 45.57
    #
    # @param [Integer] exp - amount of decimals
    # @return [Float] rounded number
    def round_up(exp = 0)
      multiplier = 10 ** exp
      ((self * multiplier).ceil).to_f/multiplier.to_f
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ruby_smart-support-1.5.0 lib/ruby_smart/support/core_ext/ruby/float.rb
ruby_smart-support-1.4.0 lib/ruby_smart/support/core_ext/ruby/float.rb
ruby_smart-support-1.3.0 lib/ruby_smart/support/core_ext/ruby/float.rb
ruby_smart-support-1.2.0 lib/ruby_smart/support/core_ext/ruby/float.rb
ruby_smart-support-1.1.1 lib/ruby_smart/support/core_ext/ruby/float.rb
ruby_smart-support-1.1.0 lib/ruby_smart/support/core_ext/ruby/float.rb
ruby_smart-support-1.0.0 lib/ruby_smart/support/core_ext/ruby/float.rb