Sha256: 4fd169f7aa55b0477971d83b8ac7f1e089a7f133f86bc04ecc1a416833162ef5

Contents?: true

Size: 1.63 KB

Versions: 18

Compression:

Stored size: 1.63 KB

Contents

module ActiveSupport
  module NumberHelper
    class RoundingHelper # :nodoc:
      attr_reader :options

      def initialize(options)
        @options = options
      end

      def round(number)
        return number unless precision
        number = convert_to_decimal(number)
        if significant && precision > 0
          round_significant(number)
        else
          round_without_significant(number)
        end
      end

      def digit_count(number)
        return 1 if number.zero?
        (Math.log10(absolute_number(number)) + 1).floor
      end

      private
        def round_without_significant(number)
          number = number.round(precision)
          number = number.to_i if precision == 0 && number.finite?
          number = number.abs if number.zero? # prevent showing negative zeros
          number
        end

        def round_significant(number)
          return 0 if number.zero?
          digits = digit_count(number)
          multiplier = 10**(digits - precision)
          (number / BigDecimal.new(multiplier.to_f.to_s)).round * multiplier
        end

        def convert_to_decimal(number)
          case number
          when Float, String
            number = BigDecimal(number.to_s)
          when Rational
            number = BigDecimal(number, digit_count(number.to_i) + precision)
          else
            number = number.to_d
          end
        end

        def precision
          options[:precision]
        end

        def significant
          options[:significant]
        end

        def absolute_number(number)
          number.respond_to?(:abs) ? number.abs : number.to_d.abs
        end
    end
  end
end

Version data entries

18 entries across 18 versions & 3 rubygems

Version Path
activesupport-5.1.7 lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.7.rc1 lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.6.2 lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.6.1 lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.6 lib/active_support/number_helper/rounding_helper.rb
tdiary-5.0.8 vendor/bundle/gems/activesupport-5.1.5/lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.5 lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.5.rc1 lib/active_support/number_helper/rounding_helper.rb
pract6-0.1.0 .gem/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.4 lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.4.rc1 lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.3 lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.3.rc3 lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.3.rc2 lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.3.rc1 lib/active_support/number_helper/rounding_helper.rb
tdiary-5.0.5 vendor/bundle/gems/activesupport-5.1.2/lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.2 lib/active_support/number_helper/rounding_helper.rb
activesupport-5.1.2.rc1 lib/active_support/number_helper/rounding_helper.rb