Sha256: cedc6e281c5cdc7a203882466a1c76b87f7a1ac882eb4cadff571fd89c7de6b2

Contents?: true

Size: 1.67 KB

Versions: 16

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

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, BigDecimal.mode(BigDecimal::ROUND_MODE))
          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(multiplier.to_f.to_s)).round * multiplier
        end

        def convert_to_decimal(number)
          case number
          when Float, String
            BigDecimal(number.to_s)
          when Rational
            BigDecimal(number, digit_count(number.to_i) + precision)
          else
            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

16 entries across 16 versions & 2 rubygems

Version Path
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/number_helper/rounding_helper.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.6.1 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.6 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.5.1 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.5 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.4.8 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.4.7 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.4.6 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.4.5 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.4.4 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.4.3 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.4.2 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.4.1 lib/active_support/number_helper/rounding_helper.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.4/lib/active_support/number_helper/rounding_helper.rb
activesupport-6.0.4 lib/active_support/number_helper/rounding_helper.rb