Class: TwitterCldr::Formatters::NumberFormatter

Inherits:
Formatter
  • 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

Instance Method Summary collapse

Constructor Details

#initialize(data_reader) ⇒ NumberFormatter

Returns a new instance of NumberFormatter.



19
20
21
# File 'lib/twitter_cldr/formatters/numbers/number_formatter.rb', line 19

def initialize(data_reader)
  @data_reader = data_reader
end

Instance Attribute Details

#data_readerObject (readonly)

Returns the value of attribute data_reader.



17
18
19
# File 'lib/twitter_cldr/formatters/numbers/number_formatter.rb', line 17

def data_reader
  @data_reader
end

Instance Method Details

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/twitter_cldr/formatters/numbers/number_formatter.rb', line 23

def format(tokens, number, options = {})
  options[:precision] ||= precision_from(number)
  options[:type] ||= :decimal

  prefix, suffix, integer_format, fraction_format = *partition_tokens(tokens)
  number = truncate_number(number, integer_format)

  int, fraction = parse_number(number, options)
  result =  integer_format.apply(int, options)
  result << fraction_format.apply(fraction, options) if fraction

  numbering_system(options[:type]).transliterate(
    "#{prefix.to_s}#{result}#{suffix.to_s}"
  )
end