Sha256: 1b3fc0e78e0d859b4727ddd0f40e5deaf0e858d8cce57899dbb5dbedba78f116

Contents?: true

Size: 1.26 KB

Versions: 32

Compression:

Stored size: 1.26 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)
        precision = absolute_precision(number)
        return number unless precision

        rounded_number = convert_to_decimal(number).round(precision, options.fetch(:round_mode, :default).to_sym)
        rounded_number.zero? ? rounded_number.abs : rounded_number # prevent showing negative zeros
      end

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

      private
        def convert_to_decimal(number)
          case number
          when Float, String
            BigDecimal(number.to_s)
          when Rational
            BigDecimal(number, digit_count(number.to_i) + options[:precision])
          else
            number.to_d
          end
        end

        def absolute_precision(number)
          if significant && options[:precision] > 0
            options[:precision] - digit_count(convert_to_decimal(number))
          else
            options[:precision]
          end
        end

        def significant
          options[:significant]
        end
    end
  end
end

Version data entries

32 entries across 32 versions & 6 rubygems

Version Path
activesupport-6.1.7.10 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.7.9 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.7.8 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.7.7 lib/active_support/number_helper/rounding_helper.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.7.6 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.7.5 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.7.4 lib/active_support/number_helper/rounding_helper.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.7.3 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.7.2 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.7.1 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.7 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.6.1 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.6 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.5.1 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.5 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.4.7 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.4.6 lib/active_support/number_helper/rounding_helper.rb
activesupport-6.1.4.5 lib/active_support/number_helper/rounding_helper.rb