Class: HumanNumber::Formatters::Number

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

Class Method Summary collapse

Class Method Details

.default_options ⇒ Object

Default options for human number formatting



62
63
64
65
66
67
68
69
# File 'lib/human_number/formatters/number.rb', line 62

def default_options
  {
    abbr_units: true,
    max_digits: nil,
    min_unit: nil,
    trim_zeros: true,
  }.freeze
end

.format(number, locale:, abbr_units:, max_digits:, min_unit:, trim_zeros:) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal formatter for numbers with intelligent abbreviations. All parameters are required - use HumanNumber.human_number for user-friendly interface.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/human_number/formatters/number.rb', line 24

def format(number, locale:, abbr_units:, max_digits:, min_unit:, trim_zeros:)
  locale = (locale || I18n.locale).to_sym

  # Direct delegation to appropriate system class
  system_class = determine_system_class(locale)
  system_class.format_number(
    number,
    locale:,
    abbr_units:,
    max_digits:,
    min_unit:,
    trim_zeros:
  )
end

.format_below_min_unit(number, locale) ⇒ Object

Format numbers below min_unit threshold with locale-appropriate delimiters



72
73
74
# File 'lib/human_number/formatters/number.rb', line 72

def format_below_min_unit(number, locale)
  NumberSystem::DefaultSystem.format_below_min_unit(number, locale)
end

.format_with_currency_precision(number, currency_code:, locale:, **options) ⇒ String

Formats a number using Rails' currency precision rules for the given currency. This ensures consistent precision regardless of display locale.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/human_number/formatters/number.rb', line 47

def format_with_currency_precision(number, currency_code:, locale:, **options)
  locale = (locale || I18n.locale).to_sym

  # Use currency's native locale for precision rules unless overridden
  precision_locale = LocaleSupport.currency_precision_locale(currency_code, locale)

  # Prepare options for number_to_currency
  currency_options = options.dup
  currency_options[:locale] = precision_locale
  currency_options[:format] = "%n" # Always format as just the number (no currency symbol)

  number_to_currency(number, **currency_options) || ZERO_STRING
end