Class: TwitterCldr::Formatters::NumberFormatter
- Defined in:
- lib/twitter_cldr/formatters/numbers/number_formatter.rb
Direct Known Subclasses
AbbreviatedNumberFormatter, CurrencyFormatter, DecimalFormatter, PercentFormatter
Constant Summary collapse
- DEFAULT_SYMBOLS =
{ :group => ',', :decimal => '.', :plus_sign => '+', :minus_sign => '-' }
Instance Attribute Summary collapse
-
#symbols ⇒ Object
readonly
Returns the value of attribute symbols.
Attributes inherited from Base
Instance Method Summary collapse
- #format(number, opts = {}) ⇒ Object
-
#initialize(options = {}) ⇒ NumberFormatter
constructor
A new instance of NumberFormatter.
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( = {}) locale = extract_locale() 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
#symbols ⇒ Object (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 |