Module: Wareki::Kansuji
- Defined in:
- lib/wareki/kansuji.rb
Overview
Utilities of kansuji/integer converter.
Constant Summary collapse
- UNIT_SUFFIX1 =
{ '千' => 1000, '百' => 100, '十' => 10, '' => 1, }.freeze
- UNIT_SUFFIX2 =
{ '京' => 10_000_000_000_000_000, '兆' => 1_000_000_000_000, '億' => 100_000_000, '万' => 10_000, '' => 1, }.freeze
Class Method Summary collapse
- .i_to_kan(num) ⇒ Object (also: i2k)
- .i_to_zen(num) ⇒ Object (also: i2z)
- .kan_to_i(str) ⇒ Object (also: k2i)
Class Method Details
.i_to_kan(num) ⇒ Object Also known as: i2k
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/wareki/kansuji.rb', line 72 def i_to_kan(num) ret = '' UNIT_SUFFIX2.each do |unit4, rank4| i4 = (num / rank4).to_i % 10_000 next if i4.zero? if i4 == 1 ret += "一#{unit4}" next end UNIT_SUFFIX1.each do |unit1, rank1| i1 = (i4 / rank1).to_i % 10 next if i1.zero? if i1 == 1 && unit1 != '' ret += unit1 else ret += i1.to_s.tr('123456789', '一二三四五六七八九') + unit1 end end ret += unit4 end ret end |
.i_to_zen(num) ⇒ Object Also known as: i2z
98 99 100 |
# File 'lib/wareki/kansuji.rb', line 98 def i_to_zen(num) num.to_s.tr('0123456789', '0123456789') end |
.kan_to_i(str) ⇒ Object Also known as: k2i
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/wareki/kansuji.rb', line 20 def kan_to_i(str) str = str.to_s.tr( '〇一二三四五六七八九0123456789零正元朔壱壹弌弐貳貮参參弎肆伍陸漆質柒捌玖拾什陌佰阡仟萬', '01234567890123456789011111122233345677789十十百百千千万' ) ret3 = 0 ret4 = 0 curnum = nil str.each_char do |c| case c when '1', '2', '3', '4', '5', '6', '7', '8', '9' if curnum curnum *= 10 else curnum = 0 end curnum += c.to_i when '0' curnum and curnum *= 10 when '卄', '廿' ret3 += 20 curnum = nil when '卅', '丗' ret3 += 30 curnum = nil when '卌' ret3 += 40 curnum = nil when '皕' ret3 += 200 curnum = nil when '万', '億', '兆', '京', '垓' if curnum ret3 += curnum curnum = nil end ret3 = 1 if ret3.zero? ret4 += ret3 * 10**((%w[万 億 兆 京 垓].index(c) + 1) * 4) ret3 = 0 when '十', '百', '千' curnum ||= 1 ret3 += curnum * 10**(%w[十 百 千].index(c) + 1) curnum = nil end end if curnum ret3 += curnum curnum = nil end ret4 + ret3 end |