Sha256: 766f385271dd79dae5d396b43ec88b8d42a40c2fabe215964dab5225ca6ca7b4

Contents?: true

Size: 1.34 KB

Versions: 50

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require "active_support/number_helper/number_converter"

module ActiveSupport
  module NumberHelper
    class NumberToCurrencyConverter < NumberConverter # :nodoc:
      self.namespace = :currency

      def convert
        format = options[:format]

        number_f = valid_float?
        if number_f
          if number_f.negative?
            number_f = number_f.abs
            format = options[:negative_format] if (number_f * 10**options[:precision]) >= 0.5
          end
          number_s = NumberToRoundedConverter.convert(number_f, options)
        else
          number_s = number.to_s.strip
          format = options[:negative_format] if number_s.sub!(/^-/, "")
        end

        format.gsub("%n", number_s).gsub("%u", options[:unit])
      end

      private
        def options
          @options ||= begin
            defaults = default_format_options.merge(i18n_opts)
            # Override negative format if format options are given
            defaults[:negative_format] = "-#{opts[:format]}" if opts[:format]
            defaults.merge!(opts)
          end
        end

        def i18n_opts
          # Set International negative format if it does not exist
          i18n = i18n_format_options
          i18n[:negative_format] ||= "-#{i18n[:format]}" if i18n[:format]
          i18n
        end
    end
  end
end

Version data entries

50 entries across 47 versions & 8 rubygems

Version Path
activesupport-7.0.8.7 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-7.0.8.6 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-7.0.8.5 lib/active_support/number_helper/number_to_currency_converter.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/number_helper/number_to_currency_converter.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/number_helper/number_to_currency_converter.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/number_helper/number_to_currency_converter.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-7.0.8.4 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-7.0.8.1 lib/active_support/number_helper/number_to_currency_converter.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/number_helper/number_to_currency_converter.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-7.1.0.rc2 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-7.1.0.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-7.1.0.beta1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-7.0.8 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-7.0.7.2 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-7.0.7.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-7.0.7 lib/active_support/number_helper/number_to_currency_converter.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/number_helper/number_to_currency_converter.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.5/lib/active_support/number_helper/number_to_currency_converter.rb