Class: Worldwide::Numbers

Inherits:
Object
  • Object
show all
Defined in:
lib/worldwide/numbers.rb

Constant Summary collapse

JAPAN_MYRIAD_UNITS =
["", "", "", "", "", "", ""].freeze
CLDR_COMPACT_STYLE =
[:short, :long].freeze
COMPACT_DECIMAL_BASE_KEY =
"numbers.latn.formats.decimal.patterns"
DEFAULT_COMPACT_PATTERN =
"0"

Instance Method Summary collapse

Constructor Details

#initialize(locale: I18n.locale) ⇒ Numbers

Returns a new instance of Numbers.



11
12
13
# File 'lib/worldwide/numbers.rb', line 11

def initialize(locale: I18n.locale)
  @locale = locale.to_sym
end

Instance Method Details

#format(amount, decimal_places: nil, humanize: nil, percent: false, relative: false) ⇒ Object



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

def format(amount, decimal_places: nil, humanize: nil, percent: false, relative: false)
  number = if percent
    amount * 100
  else
    amount
  end

  result = if CLDR_COMPACT_STYLE.include?(humanize)
    format_compact(number, decimal_places: decimal_places, style: humanize)
  else
    format_default(number, decimal_places: decimal_places, humanize: humanize)
  end

  result = add_percent_sign(result) if percent
  result = add_relative_marker(amount, result) if relative
  result
end