~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activesupport/lib/active_support/core_ext/float/rounding.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
module ActiveSupport #:nodoc:
2
 
  module CoreExtensions #:nodoc:
3
 
    module Float #:nodoc:
4
 
      module Rounding
5
 
        def self.included(base) #:nodoc:
6
 
          base.class_eval do
7
 
            alias_method :round_without_precision, :round
8
 
            alias_method :round, :round_with_precision
9
 
          end
10
 
        end
11
 
 
12
 
        # Rounds the float with the specified precision.
13
 
        #
14
 
        #   x = 1.337
15
 
        #   x.round    # => 1
16
 
        #   x.round(1) # => 1.3
17
 
        #   x.round(2) # => 1.34
18
 
        def round_with_precision(precision = nil)
19
 
          precision.nil? ? round_without_precision : (self * (10 ** precision)).round / (10 ** precision).to_f
20
 
        end
21
 
      end
22
 
    end
23
 
  end
24
 
end