Sha256: cdf6f93023ffe96c89006615be5ba9a311fb92158c803c66d29a571e84944212

Contents?: true

Size: 1.45 KB

Versions: 8

Compression:

Stored size: 1.45 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

module TwitterCldr
  module Formatters
    class CurrencyFormatter < NumberFormatter
      DEFAULT_CURRENCY_SYMBOL = "$"
      DEFAULT_PRECISION = 2

      def format(number, options = {})
        options[:currency] ||= "USD"
        currency = TwitterCldr::Shared::Currencies.for_code(options[:currency])
        currency ||= {
          :currency    => options[:currency],
          :symbol      => options[:currency],
          :cldr_symbol => options[:currency]
        }

        # overwrite with explicit symbol if given
        currency[:symbol] = options[:symbol] if options[:symbol]

        digits_and_rounding = resource(options[:currency])
        options[:precision] ||= digits_and_rounding[:digits]
        options[:rounding] ||= digits_and_rounding[:rounding]

        symbol = options[:use_cldr_symbol] ? currency[:cldr_symbol] : currency[:symbol]
        symbol ||= currency[:currency].to_s
        super(number, options).gsub('ยค', symbol)
      end

      private

      def resource(code)
        @resource ||= TwitterCldr.get_resource(:shared, :currency_digits_and_rounding)
        @resource[code.to_sym] || @resource[:DEFAULT]
      end

      def get_tokens(obj, options = {})
        opts = options.dup.merge(
          :sign => obj.abs == obj ? :positive : :negative,
          :type => :currency
        )
        @tokenizer.tokens(opts)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
twitter_cldr-2.4.3 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.0.0.beta1 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-2.4.2 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-2.4.1 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-2.4.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-2.2.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-2.1.1 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-2.1.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb