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:



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

def convert # :nodoc:
  @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)

  until (rounded_number = NumberToRoundedConverter.convert(number, options)) != NumberToRoundedConverter.convert(1000, options)
    @number = number / 1000.0
    exponent += 3
  end
  unit = determine_unit(units, exponent)
  format.gsub('%n'.freeze, rounded_number).gsub('%u'.freeze, unit).strip
end