Sha256: 500fdf5d2fb27390aecfedafc45097491ee44c93d90b6a9a1d5b4a423f5ee0be

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

# encoding: UTF-8

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

module TwitterCldr
  module Formatters
    class CurrencyFormatter < NumberFormatter

      def format(tokens, 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.gsub('ยค', symbol)
      end

      private

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

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
twitter_cldr-4.1.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-4.0.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.6.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.5.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.4.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.3.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb