Sha256: 6d82a355d34b952bedab78ff748cb418cacd27a4db548d734ea5cb106b39522f

Contents?: true

Size: 1.05 KB

Versions: 13

Compression:

Stored size: 1.05 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 initialize(options = {})
        @tokenizer = TwitterCldr::Tokenizers::NumberTokenizer.new(:locale => self.extract_locale(options), :type => :currency)
        super
      end

      def format(number, options = {})
        if options[:currency]
          currency ||= TwitterCldr::Shared::Currencies.for_code(options[:currency])
          currency ||= TwitterCldr::Shared::Currencies.for_country(options[:currency])
          currency ||= { :symbol => options[:currency] }
        else
          currency = { :symbol => DEFAULT_CURRENCY_SYMBOL }
        end

        super(number, options).gsub('ยค', currency[:symbol])
      end

      def default_format_options_for(number)
        precision = precision_from(number)
        { :precision => precision == 0 ? DEFAULT_PRECISION : precision }
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
twitter_cldr-1.9.1 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-1.9.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-1.8.1 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-1.8.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-1.7.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-1.6.2 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-1.6.1 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-1.6.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-1.5.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-1.4.1 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-1.4.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-1.3.6 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-1.3.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb