digit_delim 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 499bd98b3a7f8d5ecfe21007c4aeb977a3bbd5eb
4
- data.tar.gz: 2cb6119fe67c908c10f739ef907e0959fac6d037
3
+ metadata.gz: 625afdfd364ac8456d5ec626b34f4ab708445617
4
+ data.tar.gz: de8a149c6432e687a48dc0bae53aad19d40cf6b2
5
5
  SHA512:
6
- metadata.gz: 663574b84a12f6293f6c4278f2173f74bc3dfa401455789c00393019d945fb38dec53b0af41e57ba10ed6dc431a170e711ce477dd813ba2b833b7a9e3346b30d
7
- data.tar.gz: 7f47fa71920b86271dfe5e1d97ada2228b2bf31fde8440d09bc31e96afb0343c7475170e58f2a182743af8318d9f2203090946bad35ccbefc47a2960b95ceea8
6
+ metadata.gz: f49c625cdc792c4ec22653be8a941562fb9e692a77ecd54fbb0904fbc2b9f03be1c4abf795b4f7711772e6b4099af834344f03ba52e991e7c7c017b762ffa532
7
+ data.tar.gz: 5fcecaf3132427733fac8a825b1b04e97f981f379547a60e3837dc240f7db78de6b815be77cff427e583c4d0660009566cce2f945dd71f301c13d0fe10a1e3f8
data/Rakefile CHANGED
@@ -27,8 +27,8 @@ Jeweler::RubygemsDotOrgTasks.new
27
27
 
28
28
  require 'rake/testtask'
29
29
  Rake::TestTask.new(:test) do |test|
30
- test.libs << 'lib' << 'test'
31
- test.pattern = 'test/**/test_*.rb'
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test*.rb'
32
32
  test.verbose = true
33
33
  end
34
34
 
data/ReadMe.md CHANGED
@@ -13,7 +13,32 @@ provides the methods to separate a number with the delimiter for each 3 digits.
13
13
 
14
14
  ## Usage
15
15
 
16
- See the test code.
16
+ ```
17
+ # 漢数字 (String) を数値 (Fixnum) に変換
18
+ >> "百億".str_to_num
19
+ => 10000000000
20
+
21
+ >> "12億3400万".str_to_num
22
+ => 1234000000
23
+
24
+ # 数値を、漢数字に変換
25
+ >> "12億3400万".str_to_num.num_to_str
26
+ => "12億3400万"
27
+
28
+ # 数字文字列に3桁区切りのカンマを入れる
29
+ >> "12億3400万".str_to_num.to_s.w_dm
30
+ => "1,234,000,000"
31
+
32
+ # 4桁区切り
33
+ >> "12億3400万".str_to_num.to_s.w_dm(-1,4)
34
+ => "12,3400,0000"
35
+
36
+ # 区切り付きの数字文字列を数値 (Float) に変換
37
+ >> "12億3400万".str_to_num.to_s.w_dm(-1,4).to_wof
38
+ => 1234000000.0
39
+ ```
40
+
41
+ See the test code in `test/` for more detail.
17
42
 
18
43
  ## Lisence
19
44
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: digit_delim 0.1.0 ruby lib
5
+ # stub: digit_delim 0.2.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "digit_delim"
9
- s.version = "0.1.0"
9
+ s.version = "0.2.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["YAMAMOTO, Masayuki"]
14
- s.date = "2016-07-28"
14
+ s.date = "2016-07-30"
15
15
  s.description = "methods of num-string formatter, converter which extends String class"
16
16
  s.email = "martin.route66.blues+github@gmail.com"
17
17
  s.extra_rdoc_files = [
@@ -112,6 +112,10 @@ module KanNum
112
112
  tmp_str
113
113
  end
114
114
 
115
+ RE_NUM_O = "-.0-9"
116
+ RE_NUM = "[#{RE_NUM_O}]+"
117
+ RE_NONNUM = "[^#{RE_NUM_O} ]"
118
+
115
119
  # create parse tree for str:
116
120
  # [ [[ num, Num, ... ], UnitNum], ... ]
117
121
  #
@@ -128,11 +132,14 @@ module KanNum
128
132
  if units.include?(c)
129
133
  tmp = tmp_ary.join
130
134
  # $stderr.puts "#{__method__} tmp: #{tmp}"
131
- tmp = tmp.gsub(/([0-9]+)([^ 0-9])/, '\1 \2')
135
+ #tmp = tmp.gsub(/([0-9]+)([^ 0-9])/, '\1 \2')
136
+ tmp = tmp.gsub(/(#{RE_NUM})(#{RE_NONNUM})/, '\1 \2')
132
137
  # $stderr.puts "#{__method__} tmp: #{tmp}"
133
- tmp = tmp.gsub(/([^ 0-9])([0-9]+)/, '\1 \2')
138
+ #tmp = tmp.gsub(/([^ 0-9])([0-9]+)/, '\1 \2')
139
+ tmp = tmp.gsub(/(#{RE_NONNUM})(#{RE_NUM})/, '\1 \2')
134
140
 
135
- tmp = tmp.gsub(/([^ 0-9])([^ 0-9])/, '\1 \2')
141
+ #tmp = tmp.gsub(/([^ 0-9])([^ 0-9])/, '\1 \2')
142
+ tmp = tmp.gsub(/(#{RE_NONNUM})(#{RE_NONNUM})/, '\1 \2')
136
143
  # $stderr.puts "#{__method__} tmp: #{tmp}"
137
144
  tmp = tmp.split(/\s+/)
138
145
  # $stderr.puts "#{__method__} tmp: #{tmp}"
@@ -175,14 +182,16 @@ module KanNum
175
182
  # $stderr.puts "#{__method__} tmp_ary: #{tmp_ary}"
176
183
  a = 1
177
184
  b = N_倍数[tmp_ary[-1]]
178
- a = tmp_ary[0].to_i if tmp_ary.size > 1
185
+ #a = tmp_ary[0].to_i if tmp_ary.size > 1
186
+ a = tmp_ary[0].to_f if tmp_ary.size > 1
179
187
 
180
188
  tmp += a*b
181
189
  tmp_ary = []
182
190
  end
183
191
  end
184
192
  # $stderr.puts "#{__method__} tmp_ary: #{tmp_ary}"
185
- tmp += tmp_ary[0].to_i if tmp_ary.size != 0
193
+ #tmp += tmp_ary[0].to_i if tmp_ary.size != 0
194
+ tmp += tmp_ary[0].to_f if tmp_ary.size != 0
186
195
 
187
196
  tmp_u = N_単位数[u]
188
197
  if tmp_u.nil?
@@ -193,6 +202,11 @@ module KanNum
193
202
 
194
203
  end
195
204
 
205
+ if ret - ret.to_i > 0.0
206
+ else
207
+ ret = ret.to_i
208
+ end
209
+
196
210
  ret
197
211
  end
198
212
 
@@ -207,11 +221,71 @@ module KanNum
207
221
  ret
208
222
  end
209
223
 
224
+
225
+ #
226
+ #
227
+ #
228
+ def self.convert_helper( num )
229
+
230
+ tmp = nil
231
+ N_単位数.each_pair do |k,v|
232
+ # $stderr.puts "#{__method__} #{k} #{v}"
233
+ if num >= v
234
+ tmp ||= k
235
+ tmp = k if not(tmp.nil?) and v > N_単位数[tmp]
236
+ # $stderr.puts "#{__method__} tmp: #{tmp}"
237
+ next
238
+ else
239
+ break
240
+ end
241
+ end
242
+
243
+ tmp
244
+ end
245
+
246
+ def self.convert_phase0( num )
247
+ ret = []
248
+
249
+ tmp_num = num
250
+ while tmp = convert_helper(tmp_num)
251
+
252
+ u = N_単位数[tmp]
253
+ r = tmp_num/u
254
+ ret << [r.to_i, tmp]
255
+ tmp_num -= r*u
256
+ end
257
+
258
+ ret << tmp_num if tmp_num != 0
259
+ ret
260
+ end
261
+
262
+ def self.convert_phase1( ary )
263
+ ary.flatten.join
264
+ end
265
+
266
+ def self.num_to_str( num )
267
+
268
+ tmp = convert_phase0(num)
269
+ # $stderr.puts "{#{__method__}}result0: #{tmp}"
270
+ ret = convert_phase1(tmp)
271
+ # $stderr.puts "result1: #{ret}"
272
+
273
+ ret
274
+ end
275
+
210
276
  end
211
277
 
278
+
279
+ #
280
+ #
281
+ #
212
282
  class String
213
283
  def str_to_num; KanNum.str_to_num self; end
214
284
  end
215
285
 
286
+ class Fixnum
287
+ def num_to_str; KanNum.num_to_str self; end
288
+ end
289
+
216
290
 
217
291
  #### endof filename: .rb
@@ -1,6 +1,14 @@
1
1
  #
2
2
  # filename: test_kannum.rb
3
- # To do this test case, ``rake test``.
3
+ # To do this test case,
4
+ # ```
5
+ # rake test
6
+ # rake test TESTOPTS="--testcase=/KanNum/"
7
+ # rake test TESTOPTS="--name=/basic/"
8
+ # rake test TESTOPTS="--help"
9
+ #
10
+ # ```
11
+ #
4
12
  #
5
13
  require "helper"
6
14
  require 'test/unit'
@@ -14,18 +22,28 @@ class TestKanNum < Test::Unit::TestCase
14
22
  def setup
15
23
 
16
24
  @pattern_t1 = "12億3456万"
25
+ @pattern_n1 = 12_3456_0000
26
+
17
27
  @pattern_t2 = "100億"
28
+ @pattern_n2 = 100_0000_0000
18
29
  @pattern_t21 = "百億"
19
30
  @pattern_t22 = "100億"
20
31
 
21
32
  @pattern_t3 = "2千億3千4百十万"
33
+ @pattern_n3 = 2000_3410_0000
22
34
  @pattern_t31 = "2千億3千十万"
35
+ @pattern_n31 = 2000_3010_0000
23
36
  @pattern_t32 = "2千億千十万"
37
+ @pattern_n32 = 2000_1010_0000
24
38
 
25
39
  @pattern_t4 = "千百3十万"
40
+ @pattern_n4 = 1030_0000
26
41
  @pattern_t5 = "参拾"
27
- @pattern_t51 = "参"
28
- @pattern_t52 = ""
42
+ @pattern_n5 = 30
43
+ @pattern_t6 = ""
44
+ @pattern_n6 = 3
45
+ @pattern_t7 = "拾"
46
+ @pattern_n7 = 10
29
47
 
30
48
  end
31
49
 
@@ -39,31 +57,31 @@ class TestKanNum < Test::Unit::TestCase
39
57
  assert_equal( exp, ret )
40
58
 
41
59
  ret = @pattern_t1.str_to_num
42
- exp = 12_3456_0000
60
+ exp = @pattern_n1
43
61
  assert_equal( exp, ret )
44
62
 
45
63
  ret = KanNum.str_to_num @pattern_t2
46
- exp = 100_0000_0000
64
+ exp = @pattern_n2
47
65
  assert_equal( exp, ret )
48
66
 
49
67
  ret = KanNum.str_to_num @pattern_t21
50
- exp = 100_0000_0000
68
+ exp = @pattern_n2
51
69
  assert_equal( exp, ret )
52
70
 
53
71
  ret = KanNum.str_to_num @pattern_t22
54
- exp = 100_0000_0000
72
+ exp = @pattern_n2
55
73
  assert_equal( exp, ret )
56
74
 
57
75
  ret = KanNum.str_to_num @pattern_t3
58
- exp = 2000_3410_0000
76
+ exp = @pattern_n3
59
77
  assert_equal( exp, ret )
60
78
 
61
79
  ret = KanNum.str_to_num @pattern_t31
62
- exp = 2000_3010_0000
80
+ exp = @pattern_n31
63
81
  assert_equal( exp, ret )
64
82
 
65
83
  ret = KanNum.str_to_num @pattern_t32
66
- exp = 2000_1010_0000
84
+ exp = @pattern_n32
67
85
  assert_equal( exp, ret )
68
86
 
69
87
  ret = KanNum.str_to_num @pattern_t4
@@ -74,16 +92,28 @@ class TestKanNum < Test::Unit::TestCase
74
92
  exp = 30
75
93
  assert_equal( exp, ret )
76
94
 
77
- ret = KanNum.str_to_num @pattern_t51
95
+ ret = KanNum.str_to_num @pattern_t6
78
96
  exp = 3
79
97
  assert_equal( exp, ret )
80
98
 
81
- ret = KanNum.str_to_num @pattern_t52
99
+ ret = KanNum.str_to_num @pattern_t7
82
100
  exp = 10
83
101
  assert_equal( exp, ret )
84
102
 
85
103
  end
86
104
 
105
+ def test_str_to_num
106
+
107
+ ret = "0.7億".str_to_num
108
+ exp = 7000_0000
109
+ assert_equal( exp, ret )
110
+
111
+ ret = "-5.2億".str_to_num
112
+ exp = -5_2000_0000
113
+ assert_equal( exp, ret )
114
+
115
+ end
116
+
87
117
  def test_numable?
88
118
 
89
119
  ret = KanNum.numable? @pattern_t1
@@ -96,6 +126,51 @@ class TestKanNum < Test::Unit::TestCase
96
126
 
97
127
  end
98
128
 
129
+ def test_num_to_str
130
+
131
+ #
132
+ ret = 1_3200.num_to_str
133
+ exp = "1万3200"
134
+ assert_equal( exp, ret )
135
+
136
+ ret = @pattern_n1.num_to_str
137
+ exp = @pattern_t1
138
+ assert_equal( exp, ret )
139
+
140
+ ret = KanNum.num_to_str @pattern_n2
141
+ exp = @pattern_t2
142
+ assert_equal( exp, ret )
143
+
144
+ ret = KanNum.num_to_str @pattern_n3
145
+ exp = "2000億3410万"
146
+ assert_equal( exp, ret )
147
+
148
+ ret = @pattern_n31.num_to_str
149
+ exp = "2000億3010万"
150
+ assert_equal( exp, ret )
151
+
152
+ ret = @pattern_n32.num_to_str
153
+ exp = "2000億1010万"
154
+ assert_equal( exp, ret )
155
+
156
+ ret = @pattern_n4.num_to_str
157
+ exp = "1030万"
158
+ assert_equal( exp, ret )
159
+
160
+ ret = @pattern_n5.num_to_str
161
+ exp = "30"
162
+ assert_equal( exp, ret )
163
+
164
+ ret = @pattern_n6.num_to_str
165
+ exp = "3"
166
+ assert_equal( exp, ret )
167
+
168
+ ret = @pattern_n7.num_to_str
169
+ exp = "10"
170
+ assert_equal( exp, ret )
171
+
172
+ end
173
+
99
174
  end
100
175
 
101
176
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: digit_delim
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - YAMAMOTO, Masayuki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-28 00:00:00.000000000 Z
11
+ date: 2016-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler