Class: ActiveSupport::NumberHelper::NumberToHumanConverter
Do not use. This class is for internal use only.
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
NumberConverter
|
|
Instance Chain:
self,
NumberConverter
|
|
Inherits: |
ActiveSupport::NumberHelper::NumberConverter
|
Defined in: | activesupport/lib/active_support/number_helper/number_to_human_converter.rb |
Constant Summary
-
DECIMAL_UNITS =
# File 'activesupport/lib/active_support/number_helper/number_to_human_converter.rb', line 8{ 0 => :unit, 1 => :ten, 2 => :hundred, 3 => :thousand, 6 => :million, 9 => :billion, 12 => :trillion, 15 => :quadrillion, -1 => :deci, -2 => :centi, -3 => :mili, -6 => :micro, -9 => :nano, -12 => :pico, -15 => :femto }
-
INVERTED_DECIMAL_UNITS =
# File 'activesupport/lib/active_support/number_helper/number_to_human_converter.rb', line 10DECIMAL_UNITS.invert
NumberConverter
- Inherited
Class Attribute Summary
NumberConverter
- Inherited
Class Method Summary
NumberConverter
- Inherited
Instance Attribute Summary
NumberConverter
- Inherited
Instance Method Summary
- #convert
- #calculate_exponent(units) private
- #determine_unit(units, exponent) private
- #format private
- #unit_exponents(units) private
NumberConverter
- Inherited
Constructor Details
This class inherits a constructor from ActiveSupport::NumberHelper::NumberConverter
Instance Method Details
#calculate_exponent(units) (private)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_human_converter.rb', line 50
def calculate_exponent(units) exponent = number != 0 ? Math.log10(number.abs).floor : 0 unit_exponents(units).find { |e| exponent >= e } || 0 end
#convert
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_human_converter.rb', line 15
def convert # :nodoc: @number = RoundingHelper.new( ).round(number) @number = Float(number) # For backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files. unless .key?(:strip_insignificant_zeros) [:strip_insignificant_zeros] = true end units = opts[:units] exponent = calculate_exponent(units) @number = number / (10**exponent) rounded_number = NumberToRoundedConverter.convert(number, ) unit = determine_unit(units, exponent) format.gsub("%n", rounded_number).gsub("%u", unit).strip end
#determine_unit(units, exponent) (private)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_human_converter.rb', line 38
def determine_unit(units, exponent) exp = DECIMAL_UNITS[exponent] case units when Hash units[exp] || "" when String, Symbol I18n.translate("#{units}.#{exp}", locale: [:locale], count: number.to_i) else translate_in_locale("human.decimal_units.units.#{exp}", count: number.to_i) end end
#format (private)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_human_converter.rb', line 34
def format [:format] || translate_in_locale("human.decimal_units.format") end
#unit_exponents(units) (private)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_human_converter.rb', line 55
def unit_exponents(units) case units when Hash units when String, Symbol I18n.translate(units.to_s, locale: [:locale], raise: true) when nil translate_in_locale("human.decimal_units.units", raise: true) else raise ArgumentError, ":units must be a Hash or String translation scope." end.keys.map { |e_name| INVERTED_DECIMAL_UNITS[e_name] }.sort_by(&:-@) end