Sha256: e7755c5b2ed7fd399f52f7a203b333f89796112cfdd9e99d6b24af4e9657e659

Contents?: true

Size: 1.18 KB

Versions: 58

Compression:

Stored size: 1.18 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
        number = self.number.to_s.strip
        format = options[:format]

        if number.sub!(/^-/, "") &&
           (options[:precision] != 0 || number.to_f > 0.5)
          format = options[:negative_format]
        end

        rounded_number = NumberToRoundedConverter.convert(number, options)
        format.gsub("%n", rounded_number).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

58 entries across 58 versions & 8 rubygems

Version Path
activesupport-6.1.7.10 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.1.7.9 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.1.7.8 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.1.7.7 lib/active_support/number_helper/number_to_currency_converter.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.1.7.6 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.1.7.5 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.1.7.4 lib/active_support/number_helper/number_to_currency_converter.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/number_helper/number_to_currency_converter.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/number_helper/number_to_currency_converter.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.1.7.3 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.1.7.2 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.1.7.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.6.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.1.7 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.6 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.1.6.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.5.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.5 lib/active_support/number_helper/number_to_currency_converter.rb