chinese_numerals 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/chinese_numerals/version.rb +1 -1
- data/lib/chinese_numerals.rb +12 -5
- data/spec/chinese_number_spec.rb +4 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 824c680508fe9a51c6eb7faa7395f8845afd24e6
|
4
|
+
data.tar.gz: 9a6f26dbc6aee7a4197d1a0b496583eadfcc11fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ce7cf55503d82251ad7065832c9d54309e777c0ce5cf9ea871415eaeb50dd7f6fb96704fc60c825970cf73d0aa383089a12a00527a87c049aef46ce1e2cbe3a
|
7
|
+
data.tar.gz: 3f38015e8e77a3eb1202dcd23894e3f1351a1f4812db7a9435a5c477ed9e0f21d2462656711ebce00ddefc52ae692ee936dec321d76ebb5c3c0e34f59d444ff7
|
data/lib/chinese_numerals.rb
CHANGED
@@ -12,7 +12,8 @@ Integer.class_eval do
|
|
12
12
|
def to_chinese(args = {})
|
13
13
|
opts = {
|
14
14
|
:simple => true,
|
15
|
-
:decimal => false
|
15
|
+
:decimal => false,
|
16
|
+
:shorten => false
|
16
17
|
}.merge args
|
17
18
|
|
18
19
|
chinese_numbers = opts[:simple] ? SIMPLE_CHINESE_NUMBERS : FORMAL_CHINESE_NUMBERS
|
@@ -28,6 +29,12 @@ Integer.class_eval do
|
|
28
29
|
|
29
30
|
0.upto len - 1 do |i|
|
30
31
|
digit = str[i].to_i
|
32
|
+
|
33
|
+
if (i == 0) && (digit == 1) && (len % 4 == 2) && opts[:shorten]
|
34
|
+
res << chinese_decimals[1]
|
35
|
+
next
|
36
|
+
end
|
37
|
+
|
31
38
|
if digit == 0
|
32
39
|
zeros += 1
|
33
40
|
else
|
@@ -38,10 +45,10 @@ Integer.class_eval do
|
|
38
45
|
res << chinese_numbers[digit] + chinese_decimals[(len - 1 - i) % 4]
|
39
46
|
end
|
40
47
|
# append group name unless 4 zeros met
|
41
|
-
if (len - 1 - i) % 4 == 0
|
42
|
-
|
43
|
-
|
44
|
-
|
48
|
+
if ((len - 1 - i) % 4 == 0) && (zeros < 4)
|
49
|
+
res << CHINESE_GROUPS[(len - 1 - i) / 4]
|
50
|
+
# reset zeros for each group
|
51
|
+
zeros = 0
|
45
52
|
end
|
46
53
|
end
|
47
54
|
|
data/spec/chinese_number_spec.rb
CHANGED
@@ -34,8 +34,11 @@ describe Integer do
|
|
34
34
|
end
|
35
35
|
|
36
36
|
it "knows output and omit zeros in decimal mode" do
|
37
|
-
|
37
|
+
10000000200.to_chinese(:decimal => true).must_equal("一百億零二百")
|
38
38
|
end
|
39
39
|
|
40
|
+
it "is able to shorten the leading 1 in decimal mode" do
|
41
|
+
108000.to_chinese(:decimal => true, :shorten => true).must_equal("十萬八千")
|
42
|
+
end
|
40
43
|
end
|
41
44
|
|