chinese_capital 0.1.0 → 0.2.0
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/.gitignore +1 -0
- data/README.md +25 -2
- data/README_ZH.md +24 -2
- data/chinese_capital.gemspec +1 -0
- data/lib/chinese_capital/configuration.rb +52 -0
- data/lib/chinese_capital/number.rb +35 -27
- data/lib/chinese_capital/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4bf32bd54997da559787b84bd2d2344fde5191a
|
4
|
+
data.tar.gz: 1aacd4eb77f8c90b777dbb020d692189abcc9b46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bebf5d8f4bd37c1885f203aa320a933dfe35ee180db9d74d4005a4a609e30ff62b7cf1ff5c4f45a52c5e8a653d9692c54a109b36a0555da58a2a9509c115e227
|
7
|
+
data.tar.gz: a30f84575e9145cda0023f44e4136dc487d6d157b4202f051808a1a74105e6f6af05b42f296a697669d4b985d1b78b1a942861304e1d2b9a2dc6962e50a4d6c1
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -7,7 +7,7 @@ transform number to chinese numerals or to chinese money.
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'chinese_capital'
|
10
|
+
gem 'chinese_capital'
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
@@ -22,6 +22,7 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
transform number to chinese numerals
|
24
24
|
|
25
|
+
|
25
26
|
```ruby
|
26
27
|
ChineseCapital.parse(10103) # => 一万零一百零三
|
27
28
|
```
|
@@ -29,7 +30,29 @@ transform number to chinese numerals
|
|
29
30
|
transform number to chinese money
|
30
31
|
|
31
32
|
```ruby
|
32
|
-
ChineseCapital.
|
33
|
+
ChineseCapital.to_money(10103) # => 壹万零壹佰零叁元整
|
34
|
+
```
|
35
|
+
|
36
|
+
with custom config
|
37
|
+
|
38
|
+
In Rails direction 'config/initializers/', create file 'chinese_capital.rb', and add code:
|
39
|
+
|
40
|
+
config/initializers/chinese_capital.rb
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
ChineseCapital::Number.configure do |config|
|
44
|
+
config.normal = {
|
45
|
+
figure: '〇一二三四五六七八九'
|
46
|
+
}
|
47
|
+
config.money = {
|
48
|
+
unit: '美元'
|
49
|
+
}
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
ChineseCapital.parse(10103) # => 一万〇一百〇三
|
55
|
+
ChineseCapital.to_money(10103) # => 壹万零壹佰零叁美元整
|
33
56
|
```
|
34
57
|
|
35
58
|
## License
|
data/README_ZH.md
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'chinese_capital'
|
10
|
+
gem 'chinese_capital'
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
@@ -29,7 +29,29 @@ Or install it yourself as:
|
|
29
29
|
数字转大写金额
|
30
30
|
|
31
31
|
```ruby
|
32
|
-
ChineseCapital.
|
32
|
+
ChineseCapital.to_money(10103) # => 壹万零壹佰零叁元整
|
33
|
+
```
|
34
|
+
|
35
|
+
使用自定义配置
|
36
|
+
|
37
|
+
在Rails 'config/initializers/' 目录下, 新建文件 'chinese_capital.rb', 并添加:
|
38
|
+
|
39
|
+
config/initializers/chinese_capital.rb
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
ChineseCapital::Number.configure do |config|
|
43
|
+
config.normal = {
|
44
|
+
figure: '〇一二三四五六七八九'
|
45
|
+
}
|
46
|
+
config.money = {
|
47
|
+
unit: '美元'
|
48
|
+
}
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
ChineseCapital.parse(10103) # => 一万〇一百〇三
|
54
|
+
ChineseCapital.to_money(10103) # => 壹万零壹佰零叁美元整
|
33
55
|
```
|
34
56
|
|
35
57
|
## License
|
data/chinese_capital.gemspec
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
module ChineseCapital
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :normal, :money
|
4
|
+
|
5
|
+
NORMAL = {
|
6
|
+
figure: '零一二三四五六七八九',
|
7
|
+
minor_units: '十百千',
|
8
|
+
major_units: '万亿',
|
9
|
+
point_unit: '点',
|
10
|
+
minus_unit: '负'
|
11
|
+
}
|
12
|
+
|
13
|
+
MONEY = {
|
14
|
+
figure: '零壹贰叁肆伍陆柒捌玖',
|
15
|
+
minor_units: '拾佰仟',
|
16
|
+
major_units: '万亿',
|
17
|
+
decimal_units: '角分',
|
18
|
+
unit: '元',
|
19
|
+
round_unit: '整',
|
20
|
+
minus_unit: '负'
|
21
|
+
}
|
22
|
+
|
23
|
+
def initialize
|
24
|
+
@normal = deal_attribute('normal')
|
25
|
+
@money = deal_attribute('money')
|
26
|
+
end
|
27
|
+
|
28
|
+
def normal=(hash_normal)
|
29
|
+
@normal = deal_attribute('normal', hash_normal)
|
30
|
+
end
|
31
|
+
|
32
|
+
def money=(hash_money)
|
33
|
+
@money = deal_attribute('money', hash_money)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def deal_attribute(type, value={})
|
39
|
+
value = value.map { |k, v| [k.to_sym, v] }.to_h
|
40
|
+
attribute = if type == 'normal'
|
41
|
+
@normal = NORMAL.merge(value)
|
42
|
+
elsif type == 'money'
|
43
|
+
@money = MONEY.merge(value)
|
44
|
+
end
|
45
|
+
attribute[:figure] = attribute[:figure].strip.chars
|
46
|
+
attribute[:minor_units] = attribute[:minor_units].strip.chars.unshift(nil)
|
47
|
+
attribute[:major_units] = attribute[:major_units].strip.chars.unshift(nil)
|
48
|
+
attribute[:decimal_units] = attribute[:decimal_units].strip.chars if attribute[:decimal_units]
|
49
|
+
attribute
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -1,24 +1,29 @@
|
|
1
1
|
module ChineseCapital
|
2
2
|
require 'bigdecimal'
|
3
|
+
require 'chinese_capital/configuration'
|
3
4
|
|
4
5
|
class Number
|
5
|
-
NORMAL_FIGURE = '零一二三四五六七八九'.chars
|
6
|
-
MONEY_FIGURE = '零壹贰叁肆伍陆柒捌玖'.chars
|
7
|
-
NORMAL_MINOR_UNITS = '十百千'.chars.unshift(nil)
|
8
|
-
MONEY_MINOR_UNITS = '拾佰仟'.chars.unshift(nil)
|
9
|
-
MAJOR_UNITS = '万亿'.chars.unshift(nil)
|
10
|
-
MONEY_DECIMAL_UNITS = '角分'.chars
|
11
|
-
MONEY_UNIT = '元'.freeze
|
12
|
-
ROUND_UNIT = '整'.freeze
|
13
|
-
POINT_UNIT = '点'.freeze
|
14
|
-
MINUS_UNIT = '负'.freeze
|
15
|
-
|
16
6
|
class << self
|
7
|
+
attr_accessor :config
|
8
|
+
|
9
|
+
def config
|
10
|
+
@config ||= Configuration.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def reset
|
14
|
+
@config = Configuration.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def configure
|
18
|
+
yield(config)
|
19
|
+
end
|
20
|
+
|
17
21
|
def parse(num)
|
18
22
|
result = []
|
19
23
|
b_num = BigDecimal(num.to_s)
|
20
24
|
|
21
|
-
|
25
|
+
normal_zero = config.normal[:figure][0]
|
26
|
+
return normal_zero if b_num.zero?
|
22
27
|
|
23
28
|
temp = b_num.abs.divmod(1)
|
24
29
|
round_section = temp[0].to_s('F').to_i
|
@@ -26,19 +31,21 @@ module ChineseCapital
|
|
26
31
|
|
27
32
|
result_round_section = round_section_zh(round_section)
|
28
33
|
result << result_round_section unless result_round_section.empty?
|
29
|
-
result <<
|
30
|
-
result <<
|
34
|
+
result << normal_zero if temp[1] != 0 && result.empty?
|
35
|
+
result << config.normal[:point_unit] unless temp[1].zero?
|
31
36
|
result << decimal_section_zh(decimal_section) unless temp[1].zero?
|
32
|
-
result.unshift(
|
37
|
+
result.unshift(config.normal[:minus_unit]) if b_num < 0
|
33
38
|
result.join
|
34
39
|
end
|
35
40
|
|
36
41
|
def to_money(num)
|
37
42
|
result = []
|
43
|
+
|
38
44
|
# 金额只保留前两位小数
|
39
45
|
b_num = BigDecimal(num.to_s).truncate(2)
|
40
46
|
|
41
|
-
|
47
|
+
money_zero = config.money[:figure][0]
|
48
|
+
return "#{money_zero}#{config.money[:unit]}#{config.money[:round_unit]}" if b_num.zero?
|
42
49
|
|
43
50
|
temp = b_num.abs.divmod(1)
|
44
51
|
round_section = temp[0].to_s('F').to_i
|
@@ -46,14 +53,14 @@ module ChineseCapital
|
|
46
53
|
|
47
54
|
result_round_section = round_section_zh(round_section, 'money')
|
48
55
|
result << result_round_section unless result_round_section.empty?
|
49
|
-
result <<
|
56
|
+
result << config.money[:unit] unless result_round_section.empty?
|
50
57
|
unless temp[1].zero?
|
51
58
|
result_decimal_section = decimal_section_zh(decimal_section, 'money')
|
52
|
-
result_decimal_section.gsub!(/(?<=\A|["#{
|
59
|
+
result_decimal_section.gsub!(/(?<=\A|["#{money_zero}"])["#{money_zero}"]+/, '') if result.empty?
|
53
60
|
result << result_decimal_section
|
54
61
|
end
|
55
|
-
result <<
|
56
|
-
result.unshift(
|
62
|
+
result << config.money[:round_unit] if BigDecimal(decimal_section).zero?
|
63
|
+
result.unshift(config.money[:minus_unit]) if b_num < 0 && result
|
57
64
|
result.join
|
58
65
|
end
|
59
66
|
|
@@ -63,8 +70,8 @@ module ChineseCapital
|
|
63
70
|
result = []
|
64
71
|
unit_index = -1
|
65
72
|
|
66
|
-
figure_type =
|
67
|
-
unit_type =
|
73
|
+
figure_type = config.send(parse_type.to_sym)[:figure]
|
74
|
+
unit_type = config.send(parse_type.to_sym)[:minor_units]
|
68
75
|
|
69
76
|
num.to_s.chars.map(&:to_i).reverse.each_slice(4) do |figures|
|
70
77
|
section = figures.zip(unit_type).map do |figure, unit|
|
@@ -82,21 +89,22 @@ module ChineseCapital
|
|
82
89
|
|
83
90
|
next if section.empty?
|
84
91
|
|
85
|
-
result << round_section_unit(unit_index)
|
92
|
+
result << round_section_unit(unit_index, parse_type)
|
86
93
|
result << section
|
87
94
|
end
|
88
95
|
|
89
96
|
result.reverse.join.gsub(/(?<=\A|["#{figure_type[0]}"])["#{figure_type[0]}"]+/, '')
|
90
97
|
end
|
91
98
|
|
92
|
-
def round_section_unit(unit_index)
|
93
|
-
|
99
|
+
def round_section_unit(unit_index, parse_type)
|
100
|
+
major_units = config.send(parse_type.to_sym)[:major_units]
|
101
|
+
major_units[1] * (unit_index % 2) + major_units[2] * (unit_index / 2)
|
94
102
|
end
|
95
103
|
|
96
104
|
def decimal_section_zh(num, parse_type='normal')
|
97
|
-
figure_type =
|
105
|
+
figure_type = config.send(parse_type.to_sym)[:figure]
|
98
106
|
figures = if parse_type == 'money'
|
99
|
-
num.chars.map(&:to_i).zip(
|
107
|
+
num.chars.map(&:to_i).zip(config.money[:decimal_units])
|
100
108
|
else
|
101
109
|
num.chars.map(&:to_i).zip([])
|
102
110
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chinese_capital
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jarck-lou
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: transform number to chinese numerals or to chinese money.
|
42
56
|
email:
|
43
57
|
- loujiandong@gmail.com
|
@@ -56,6 +70,7 @@ files:
|
|
56
70
|
- bin/setup
|
57
71
|
- chinese_capital.gemspec
|
58
72
|
- lib/chinese_capital.rb
|
73
|
+
- lib/chinese_capital/configuration.rb
|
59
74
|
- lib/chinese_capital/number.rb
|
60
75
|
- lib/chinese_capital/version.rb
|
61
76
|
homepage: https://github.com/jarck/chinese_capital
|