Class: TwitterCldr::Formatters::CurrencyFormatter

Inherits:
NumberFormatter show all
Defined in:
lib/twitter_cldr/formatters/numbers/currency_formatter.rb

Constant Summary collapse

DEFAULT_CURRENCY_SYMBOL =
"$"
DEFAULT_PRECISION =
2

Constants inherited from NumberFormatter

NumberFormatter::DEFAULT_SYMBOLS

Instance Attribute Summary

Attributes inherited from NumberFormatter

#symbols

Attributes inherited from Base

#tokenizer

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CurrencyFormatter

Returns a new instance of CurrencyFormatter.


12
13
14
15
# File 'lib/twitter_cldr/formatters/numbers/currency_formatter.rb', line 12

def initialize(options = {})
  @tokenizer = TwitterCldr::Tokenizers::NumberTokenizer.new(:locale => self.extract_locale(options), :type => :currency)
  super
end

Instance Method Details

#default_format_options_for(number) ⇒ Object


29
30
31
32
# File 'lib/twitter_cldr/formatters/numbers/currency_formatter.rb', line 29

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

#format(number, options = {}) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/twitter_cldr/formatters/numbers/currency_formatter.rb', line 17

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