Class: TwitterCldr::Formatters::NumberFormatter

Inherits:
Base
  • Object
show all
Defined in:
lib/twitter_cldr/formatters/numbers/number_formatter.rb

Constant Summary collapse

DEFAULT_SYMBOLS =
{ :group => ',', :decimal => '.', :plus_sign => '+', :minus_sign => '-' }

Instance Attribute Summary collapse

Attributes inherited from Base

#tokenizer

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ NumberFormatter

Returns a new instance of NumberFormatter.



13
14
15
16
17
18
19
20
# File 'lib/twitter_cldr/formatters/numbers/number_formatter.rb', line 13

def initialize(options = {})
  locale = extract_locale(options)
  cache_key = TwitterCldr::Utils.compute_cache_key(locale)
  @tokenizer = tokenizer_cache[cache_key] ||= TwitterCldr::Tokenizers::NumberTokenizer.new(
    :locale => locale
  )
  @symbols = DEFAULT_SYMBOLS.merge(tokenizer.symbols)
end

Instance Attribute Details

#symbolsObject (readonly)

Returns the value of attribute symbols.



9
10
11
# File 'lib/twitter_cldr/formatters/numbers/number_formatter.rb', line 9

def symbols
  @symbols
end

Instance Method Details

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



22
23
24
25
26
27
28
29
30
31
# File 'lib/twitter_cldr/formatters/numbers/number_formatter.rb', line 22

def format(number, opts = {})
  opts[:precision] ||= precision_from(number)
  prefix, suffix, integer_format, fraction_format = *partition_tokens(get_tokens(number, opts))
  number = transform_number(number)

  int, fraction = parse_number(number, opts)
  result =  integer_format.apply(int, opts)
  result << fraction_format.apply(fraction, opts) if fraction
  "#{prefix.to_s}#{result}#{suffix.to_s}"
end