class Integer
Constants
- TEN_NUMERALS
- UNITS_GREATER_THAN_TEN_THOUSAND
- UNITS_UNTIL_THOUSAND
Public Instance Methods
to_chinese_numeral()
click to toggle source
# 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
Private Instance Methods
convert_separated_by_four_digit(string, char_count, index = 0, limit = 0)
click to toggle source
# File lib/convert_numeral.rb, line 34 def convert_separated_by_four_digit(string, char_count, index = 0, limit = 0) (1..char_count).inject('') do |current_string, digit| numeral = string[-digit].to_i thousand_numeral = UNITS_UNTIL_THOUSAND[digit - 2] add_string = if digit == 1 numeral > 0 ? TEN_NUMERALS[numeral] : '' elsif digit == 4 if index < limit numeral > 0 ? TEN_NUMERALS[numeral] + thousand_numeral : '' else numeral > 1 ? TEN_NUMERALS[numeral] + thousand_numeral : thousand_numeral end else if numeral == 0 '' elsif numeral == 1 thousand_numeral else TEN_NUMERALS[numeral] + thousand_numeral end end add_string + current_string end end
extract_numerals_separated_by_four_digit(string, limit, mod)
click to toggle source
# File lib/convert_numeral.rb, line 61 def extract_numerals_separated_by_four_digit(string, limit, mod) (0..limit).map do |index| if index < limit string[-(index * 4 + 4), 4] elsif index == limit if mod == 0 string[-(index * 4 + 4), 4] else string[-(index * 4 + mod), mod] end end end end