Class: Integer

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

Constant Summary collapse

UNITS_GREATER_THAN_TEN_THOUSAND =
[''].concat(%w(     𥝱       恒河沙 阿僧祇 那由他 不可思議 無量大数)).map(&:freeze).freeze
UNITS_UNTIL_THOUSAND =
%w(  ).map(&:freeze).freeze
TEN_NUMERALS =
%w(         ).map(&:freeze).freeze

Instance Method Summary collapse

Instance Method Details

#to_chinese_numeralObject



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

def to_chinese_numeral
  return TEN_NUMERALS[0] if self == 0

  string = self.to_s
  total_length = string.length

  return convert_separated_by_four_digit(string, total_length) if total_length <= 4

  quo, mod = total_length.divmod(4)
  limit = (mod == 0) ? quo - 1 : quo
  separated_numerals_by_four_digit = extract_numerals_separated_by_four_digit(string, limit, mod)

  separated_numerals_by_four_digit
    .map.with_index do |numeral, index|
      converted_string = convert_separated_by_four_digit(numeral, numeral.length, index, limit)
      converted_string.empty? ? '' : converted_string + UNITS_GREATER_THAN_TEN_THOUSAND[index]
    end
    .reverse
    .join
end