number_to_cn 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.DS_Store CHANGED
Binary file
data/README.md CHANGED
@@ -22,13 +22,13 @@ so simple to use:
22
22
 
23
23
  require 'number_to_cn'
24
24
 
25
- 123.to_cn_words #=>
25
+ 123.to_cn_words #=> 壹佰贰拾叁
26
26
 
27
- 123.to_cn_clearly #=>
27
+ 123.to_cn_clearly #=> 壹贰叁
28
28
 
29
- 0.1.to_cn_words #=>
29
+ 0.1.to_cn_words #=> 零点壹
30
30
 
31
- 0.to_cn_words #=>
31
+ 0.to_cn_words #=>
32
32
 
33
33
  ## Contributing
34
34
 
@@ -1,59 +1,59 @@
1
1
  #encoding:utf-8
2
2
  module NumberToCn
3
- CN_T_TRANS = [ "", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾" ]
3
+ CN_T_TRANS = [ "", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾" ]
4
4
  CN_T_TRANS_WITH_ZERO = [ "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾" ]
5
- CN_T_POSITION = [ "", "拾", "佰", "仟" ]
6
- CN_T_BIG = [ "", "萬", "亿", "萬"]
5
+ CN_T_POSITION = [ "", "拾", "佰", "仟" ]
6
+ CN_T_BIG = [ "", "萬", "亿", "萬" ]
7
7
 
8
8
  def to_cn_words
9
9
  if self.class == Fixnum
10
- return "零" if self == 0
11
- num_arr = self.to_s.split("").reverse
12
- rst_arr = []
13
-
14
- writable = -1
15
- #1:0是需要写的。
16
- #0:0不需要写;
17
- #-1:全不需要写;
18
-
19
- num_arr.each_with_index do |value, index|
20
- writable = -1 if index%4 == 0
21
-
22
- cc = (index % 4 == 0) ? CN_T_BIG[index/4] : ""
23
- aa = CN_T_TRANS[value.to_i]
24
-
25
- if value.to_i == 0
26
- writable = (writable == 1) ? 0 : -1
27
- else
28
- writable = 1
29
- end
30
-
31
- if writable == 1
32
- bb = CN_T_POSITION[index%4]
33
- elsif writable == -1
34
- bb = ""
35
- else
36
- bb = "零"
37
- end
38
-
39
- rst_arr << aa + bb + cc
10
+ int_words
11
+ elsif self.class == Float
12
+ float_words
13
+ end
14
+ end
15
+
16
+ def int_words
17
+ return "零" if self == 0
18
+
19
+ num_arr = self.to_s.split('').reverse
20
+ rst_arr = []
21
+
22
+ writable = -1
23
+ #1: 都需要写;
24
+ #0: 写一个零,后面的'千'不用写;
25
+ #-1:不需要写'千',也不需要写'零';
26
+
27
+ num_arr.each_with_index do |value, index|
28
+ writable = -1 if index%4 == 0
29
+ cc = (index%4 == 0) ? CN_T_BIG[index/4] : ""
30
+ aa = CN_T_TRANS[value.to_i]
31
+
32
+ if value.to_i == 0
33
+ bb = (writable == 1) ? "零" : ""
34
+ writable = (writable == 1) ? 0 : -1
35
+ else
36
+ bb = CN_T_POSITION[index%4]
37
+ writable = 1
40
38
  end
41
39
 
42
- rst_arr.reverse.join
43
- elsif self.class == Float
44
- before_point = self.to_s.split(".")[0]
45
- after_point = self.to_s.split(".")[1]
46
- "#{before_point.to_i.to_cn_words}点#{after_point.to_i.to_cn_clearly}"
40
+ rst_arr << aa + bb + cc
47
41
  end
42
+
43
+ rst_arr.reverse.join
44
+ end
45
+
46
+ def float_words
47
+ before_point = self.to_i
48
+ after_point = self.to_s.split(".")[1]
49
+ after_point_zero = "零" * (after_point.length - after_point.to_i.to_s.length)
50
+ "#{before_point.int_words}点#{after_point_zero}#{after_point.to_i.to_cn_clearly}"
48
51
  end
49
52
 
50
53
  def to_cn_clearly
51
- digits_arr = self.to_s.split('')
52
- rst = ""
53
- digits_arr.map do |d|
54
- rst << CN_T_TRANS_WITH_ZERO[d.to_i]
55
- end
56
- rst
54
+ to_s.split('')
55
+ .delete_if{ |c| c =~ /\D/ }
56
+ .map{ |c| CN_T_TRANS_WITH_ZERO[c.to_i] }
57
+ .join
57
58
  end
58
-
59
59
  end
@@ -1,3 +1,3 @@
1
1
  module NumberToCn
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -6,10 +6,34 @@ describe NumberToCn do
6
6
  (2203.to_cn_words).should eq("贰仟贰佰零叁")
7
7
  end
8
8
 
9
+ it "should go works for 2003" do
10
+ (2003.to_cn_words).should eq("贰仟零叁")
11
+ end
12
+
13
+ it "should go works for 20003" do
14
+ (20003.to_cn_words).should eq("贰萬零叁")
15
+ end
16
+
17
+ it "should go works for 200003" do
18
+ (200003.to_cn_words).should eq("贰拾萬零叁")
19
+ end
20
+
9
21
  it "worked for zero" do
10
22
  (0.to_cn_words).should eq("零")
11
23
  end
12
24
 
25
+ it "worked for 0.0" do
26
+ (0.0.to_cn_words).should eq("零点零")
27
+ end
28
+
29
+ it "worked for 0.003" do
30
+ (0.003.to_cn_words).should eq("零点零零叁")
31
+ end
32
+
33
+ it "worked for 0.0" do
34
+ (0.02.to_cn_words).should eq("零点零贰")
35
+ end
36
+
13
37
  it "worked for float" do
14
38
  ((33.0).to_cn_words).should eq("叁拾叁点零")
15
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: number_to_cn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-19 00:00:00.000000000 Z
12
+ date: 2013-09-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: simply gem for put digits number into cn.
15
15
  email:
@@ -53,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  version: '0'
54
54
  requirements: []
55
55
  rubyforge_project:
56
- rubygems_version: 1.8.24
56
+ rubygems_version: 1.8.23
57
57
  signing_key:
58
58
  specification_version: 3
59
59
  summary: ! '123.to_cn_words #'