Sha256: ef792cb2431f0f2e9d010e55538edf9641ca3b3d34fed3a33811eaf62e8844bf

Contents?: true

Size: 1.2 KB

Versions: 84

Compression:

Stored size: 1.2 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 options[:significant] && options[:precision] > 0
            options[:precision] - digit_count(convert_to_decimal(number))
          else
            options[:precision]
          end
        end
    end
  end
end

Version data entries

84 entries across 80 versions & 11 rubygems

Version Path
activesupport-7.2.1 lib/active_support/number_helper/rounding_helper.rb
activesupport-7.2.0 lib/active_support/number_helper/rounding_helper.rb
activesupport-7.2.0.rc1 lib/active_support/number_helper/rounding_helper.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/number_helper/rounding_helper.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/number_helper/rounding_helper.rb
activesupport-7.2.0.beta3 lib/active_support/number_helper/rounding_helper.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/number_helper/rounding_helper.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/number_helper/rounding_helper.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/number_helper/rounding_helper.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/number_helper/rounding_helper.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/number_helper/rounding_helper.rb
activesupport-7.2.0.beta2 lib/active_support/number_helper/rounding_helper.rb
activesupport-7.1.3.4 lib/active_support/number_helper/rounding_helper.rb
activesupport-7.0.8.4 lib/active_support/number_helper/rounding_helper.rb
activesupport-7.2.0.beta1 lib/active_support/number_helper/rounding_helper.rb
activesupport-7.1.3.2 lib/active_support/number_helper/rounding_helper.rb
activesupport-7.1.3.1 lib/active_support/number_helper/rounding_helper.rb
activesupport-7.0.8.1 lib/active_support/number_helper/rounding_helper.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3/lib/active_support/number_helper/rounding_helper.rb
activesupport-7.1.3 lib/active_support/number_helper/rounding_helper.rb