Module: YaKansuji::Formatter::Simple

Defined in:
lib/ya_kansuji/formatter/simple.rb

Class Method Summary collapse

Class Method Details

.call(num, _options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ya_kansuji/formatter/simple.rb', line 9

def call(num, _options = {})
  return '' if num.zero?

  ret = ''
  (UNIT_EXP4.reverse + ['']).each_with_index do |unit4, ridx4|
    i4 = (num / 10_000**(UNIT_EXP4.size - ridx4)).to_i % 10_000
    next if i4.zero?

    if i4 == 1
      ret += "#{unit4}"
      next
    end
    (UNIT_EXP3.reverse + ['']).each_with_index do |unit3, ridx3|
      i3 = (i4 / 10**(UNIT_EXP3.size - ridx3)).to_i % 10
      next if i3.zero?

      if i3 == 1 && unit3 != ''
        ret += unit3
      else
        ret += i3.to_s.tr('123456789', '一二三四五六七八九') + unit3
      end
    end
    ret += unit4
  end
  ret
end