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
# File 'lib/twitter_cldr/formatters/numbers/number_formatter.rb', line 13

def initialize(options = {})
  @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, options = {}) ⇒ Object



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

def format(number, options = {})
  opts = self.default_format_options_for(number).merge(options)
  prefix, suffix, integer_format, fraction_format = *partition_tokens(self.get_tokens(number, opts))
  
  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