Sha256: c01abe910f85262e2c5edc7fde0edf7ae1eb9adb02aea5255aa7b9b2edfebde1

Contents?: true

Size: 1.24 KB

Versions: 5

Compression:

Stored size: 1.24 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

module TwitterCldr
  module Formatters
    class AbbreviatedNumberFormatter < NumberFormatter
      NUMBER_MAX = 10 ** (TwitterCldr::Tokenizers::NumberTokenizer::ABBREVIATED_MAX_POWER + 1)
      NUMBER_MIN = 10 ** TwitterCldr::Tokenizers::NumberTokenizer::ABBREVIATED_MIN_POWER

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

      def get_tokens(obj, options = {})
        type = (obj < NUMBER_MAX) && (obj >= NUMBER_MIN) ? get_type : :decimal
        format = type == get_type ? get_key(obj) : nil
        opts = options.merge(
          :sign => obj.abs == obj ? :positive : :negative,
          :type => type,
          :format => format
        )
        @tokenizer.tokens(opts)
      end

      protected

      def get_type
        :decimal
      end

      def get_key(number)
        "1#{"0" * (number.to_i.to_s.size - 1)}".to_i
      end

      def transform_number(number)
        if (number < NUMBER_MAX) && (number >= NUMBER_MIN)
          power = (((number.to_s.length - 1) / 3) * 3).floor
          factor = (10 ** power).to_f
          number / factor
        else
          number
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
twitter_cldr-2.4.3 lib/twitter_cldr/formatters/numbers/abbreviated/abbreviated_number_formatter.rb
twitter_cldr-3.0.0.beta1 lib/twitter_cldr/formatters/numbers/abbreviated/abbreviated_number_formatter.rb
twitter_cldr-2.4.2 lib/twitter_cldr/formatters/numbers/abbreviated/abbreviated_number_formatter.rb
twitter_cldr-2.4.1 lib/twitter_cldr/formatters/numbers/abbreviated/abbreviated_number_formatter.rb
twitter_cldr-2.4.0 lib/twitter_cldr/formatters/numbers/abbreviated/abbreviated_number_formatter.rb