File: active_support/core_ext/float/rounding.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: ActiveSupport#1
  module: CoreExtensions#2
  module: Float#3
  module: Rounding#4
has properties
module method: included / 1 #5
method: round_with_precision / 1 #18

Code

   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