Class: ActiveSupport::NumberHelper::NumberToHumanConverter

Inherits:
NumberConverter show all
Defined in:
lib/active_support/number_helper/number_to_human_converter.rb

Overview

:nodoc:

Constant Summary collapse

DECIMAL_UNITS =
{ 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 =
DECIMAL_UNITS.invert

Constants inherited from NumberConverter

ActiveSupport::NumberHelper::NumberConverter::DEFAULTS

Instance Attribute Summary

Attributes inherited from NumberConverter

#number, #opts

Instance Method Summary collapse

Methods inherited from NumberConverter

convert, #execute, #initialize

Constructor Details

This class inherits a constructor from ActiveSupport::NumberHelper::NumberConverter

Instance Method Details

#convertObject

:nodoc:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_support/number_helper/number_to_human_converter.rb', line 15

def convert # :nodoc:
  @number = RoundingHelper.new(options).round(number)
  @number = Float(number)

  # For backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files.
  unless options.key?(:strip_insignificant_zeros)
    options[:strip_insignificant_zeros] = true
  end

  units = opts[:units]
  exponent = calculate_exponent(units)
  @number = number / (10**exponent)

  rounded_number = NumberToRoundedConverter.convert(number, options)
  unit = determine_unit(units, exponent)
  format.gsub("%n", rounded_number).gsub("%u", unit).strip
end