Class: ReeNumber::NumberToHuman

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_human.rb

Constant Summary collapse

DEFAULTS =
{
  units: "decimal_units",
  locale: :en,
  format: "%n %u",
  precision: 3,
  significant: true,
  strip_insignificant_zeros: true,
  separator: ".",
  delimiter: "",
  round_mode: :default
}.freeze

Instance Method Summary collapse

Instance Method Details

#call(number, **opts) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_human.rb', line 141

def call(number, **opts)
  options = DEFAULTS.merge(opts)

  number = round_helper(
    number,
    **slice(options, [:precision, :significant, :round_mode])
  )

  number = Float(number)

  exponent = calculate_exponent(
    number, options[:locale], options[:units]
  )

  number = number / (10**exponent)

  rounded_number = number_to_rounded(
    number,
    **slice(
      options,
      [:precision, :significant, :strip_insignificant_zeros, :round_mode]
    )
  )

  unit = determine_unit(
    exponent, options[:units], options[:locale]
  )

  result_number = options[:format]
    .gsub("%n", rounded_number)
    .gsub("%u", unit)
    .strip

  number_to_delimited(
    result_number,
    **slice(options, [:separator, :delimiter])
  )
end