Module: NumberToCn

Included in:
Float, Integer
Defined in:
lib/number_to_cn/methods.rb,
lib/number_to_cn/version.rb

Constant Summary collapse

CN_T_TRANS =
[ "", "", "", "", "", "", "", "", "", "", "" ]
CN_T_TRANS_WITH_ZERO =
[ "", "", "", "", "", "", "", "", "", "", "" ]
CN_T_POSITION =
[ "", "", "", "" ]
CN_T_BIG =
[ "", "", "亿", ""]
VERSION =
"0.0.4"

Instance Method Summary collapse

Instance Method Details

#to_cn_clearlyObject



50
51
52
53
54
55
56
57
# File 'lib/number_to_cn/methods.rb', line 50

def to_cn_clearly
  digits_arr = self.to_s.split('')
  rst = ""
  digits_arr.map do |d|
    rst << CN_T_TRANS_WITH_ZERO[d.to_i]
  end
  rst
end

#to_cn_wordsObject



8
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/number_to_cn/methods.rb', line 8

def to_cn_words
  if self.class == Fixnum
    return "" if self == 0
    num_arr = self.to_s.split("").reverse
    rst_arr = []

    writable = -1 
    #1:0是需要写的。
    #0:0不需要写; 
    #-1:全不需要写;

    num_arr.each_with_index do |value, index|
      writable = -1 if index%4 == 0

      cc = (index % 4 == 0) ? CN_T_BIG[index/4] : ""
      aa = CN_T_TRANS[value.to_i]

      if value.to_i == 0
        writable = (writable == 1) ? 0 : -1
      else
        writable = 1
      end

      if writable == 1
        bb = CN_T_POSITION[index%4]
      elsif writable == -1
        bb = ""
      else
        bb = ""
      end

      rst_arr << aa + bb + cc
    end

    rst_arr.reverse.join
  elsif self.class == Float
    before_point = self.to_s.split(".")[0]
    after_point = self.to_s.split(".")[1]
    "#{before_point.to_i.to_cn_words}#{after_point.to_i.to_cn_clearly}"
  end
end