Module: ActiveSupport::CoreExtensions::Float::Rounding

Included in:
Float
Defined in:
lib/gems/activesupport-2.2.2/lib/active_support/core_ext/float/rounding.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



5
6
7
8
9
10
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/float/rounding.rb', line 5

def self.included(base) #:nodoc:
  base.class_eval do
    alias_method :round_without_precision, :round
    alias_method :round, :round_with_precision
  end
end

Instance Method Details

#round_with_precision(precision = nil) ⇒ Object

Rounds the float with the specified precision.

x = 1.337
x.round    # => 1
x.round(1) # => 1.3
x.round(2) # => 1.34


18
19
20
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/float/rounding.rb', line 18

def round_with_precision(precision = nil)
  precision.nil? ? round_without_precision : (self * (10 ** precision)).round / (10 ** precision).to_f
end