Class: HumanNumber::NumberSystem::DefaultSystem

Inherits:
Object
  • Object
show all
Extended by:
ActionView::Helpers::NumberHelper
Defined in:
lib/human_number/number_system.rb

Overview

Western/Default number formatting system using standard thousand-based units.

This system implements the standard Western number formatting convention using powers of 1000 progression: thousand, million, billion, trillion. It's used by most Western languages and global financial contexts.

Examples:

Formatting examples

# Via HumanNumber.human_number (using :en locale)
HumanNumber.human_number(1_234)         #=> "1K 234"
HumanNumber.human_number(1_234_567)     #=> "1M 234K 567"
HumanNumber.human_number(1_234_567_890) #=> "1B 234M 567K 890"

# Abbreviated mode
HumanNumber.human_number(1_234_567, max_digits: 2) #=> "1.2M"

Supported locales

# European languages: English, German, French, Spanish, Italian, etc.
[:en, :de, :fr, :es, :it, :pt, :ru, :nl, :sv, :da, :no]

See Also:

Since:

  • 0.1.10

Direct Known Subclasses

EastAsianSystem, IndianSystem

Defined Under Namespace

Classes: UnitBreakdown

Constant Summary collapse

UNIT_DEFINITIONS =

Since:

  • 0.1.10

[
  { key: :trillion, divisor: 1_000_000_000_000 },
  { key: :billion, divisor: 1_000_000_000 },
  { key: :million, divisor: 1_000_000 },
  { key: :thousand, divisor: 1_000 },
].freeze

Class Method Summary collapse

Class Method Details

.format_below_min_unit(number, locale) ⇒ Object

Format numbers below min_unit threshold with locale-appropriate delimiters

Since:

  • 0.1.10



256
257
258
# File 'lib/human_number/number_system.rb', line 256

def format_below_min_unit(number, locale)
  number_with_delimiter(number, locale: locale) || number.to_s
end

.format_in_abbreviated_mode(number, locale, abbr_units, max_digits, trim_zeros, _min_unit) ⇒ Object

Since:

  • 0.1.10



247
248
249
250
251
252
253
# File 'lib/human_number/number_system.rb', line 247

def format_in_abbreviated_mode(number, locale, abbr_units, max_digits, trim_zeros, _min_unit)
  # Abbreviated mode shows single largest unit: "1.2M"
  unit_breakdown = break_down_into_largest_unit(number)
  return nil if unit_breakdown.empty?

  format_abbreviated_unit_parts(unit_breakdown, locale, abbr_units, max_digits, trim_zeros)
end

.format_in_complete_mode(number, locale, abbr_units, _min_unit) ⇒ Object

Since:

  • 0.1.10



239
240
241
242
243
244
245
# File 'lib/human_number/number_system.rb', line 239

def format_in_complete_mode(number, locale, abbr_units, _min_unit)
  # Complete mode shows all units: "1M 234K 567"
  unit_breakdown = break_down_into_all_units(number)
  return nil if unit_breakdown.empty?

  format_all_unit_parts(unit_breakdown, locale, abbr_units)
end

.format_number(number, locale:, abbr_units: true, min_unit: nil, max_digits: 2, trim_zeros: true) ⇒ Object

Since:

  • 0.1.10



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/human_number/number_system.rb', line 222

def format_number(number, locale:, abbr_units: true, min_unit: nil, max_digits: 2, trim_zeros: true)
  return ZERO_STRING if number.zero?

  # Check if number meets minimum unit threshold for human formatting
  return format_below_min_unit(number, locale) if min_unit && number.abs < min_unit

  parts = if max_digits.nil?
            format_in_complete_mode(number, locale, abbr_units, min_unit)
          else
            format_in_abbreviated_mode(number, locale, abbr_units, max_digits, trim_zeros, min_unit)
          end

  return number.to_s if parts.nil?

  finalize_result(number, parts)
end