digit_delim 0.0.1 → 0.3.1

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
- SHA1:
3
- metadata.gz: e50f4a154d5c40562dfebe807077312afc745822
4
- data.tar.gz: 9bf8033451bba472ca8b84d282ab070348e7feae
2
+ SHA256:
3
+ metadata.gz: c51d754ec57148df049690c2feb6a1b92b8e32060e615bc00f3104c32d71df0e
4
+ data.tar.gz: d16c5b56b82618fce3a016c9d852c08af8187a4fd24a936ed47f75cbccb742aa
5
5
  SHA512:
6
- metadata.gz: 6683d34ae966505dc8b27d786e6beb54ad9a637ea4c8e9e39fdecd0789679f24c0a7938d97521c398a2fc798f9dc9b1ea54d06db5a48aaa7145ac6f294e9cae4
7
- data.tar.gz: 329f504f02f0683473a5e08e073f2e22bf2305a45a677685909a48de91fb3eec2c337ebf71436ea8325cd55f4e78dc5ef3215af3a235036fa4f340263c0012b4
6
+ metadata.gz: e0af8d0a652a09cf6b7c04334e81528ed46e4e6f10c28a7eef9609119f59839b501a11f429f775c0b1d1be0dac1e716a9880acfd53cc64e05ed626d0d6603171
7
+ data.tar.gz: 9d13bd89793cc9d271feee1c710b8fe566c1d82b56c43d5caef3ba4eeaf4cc5b1b9ac1c8d65815dec5088ceb676efcbc170ac2f4fb7e7d07bc53604d9f2ec0d0
data/Gemfile CHANGED
@@ -6,9 +6,12 @@ source "http://rubygems.org"
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
9
- gem "shoulda", ">= 0"
10
- gem "rdoc", "~> 3.12"
11
- gem "bundler", "~> 1.3.5"
12
- gem "jeweler", "~> 1.8.4"
13
- gem "simplecov-rcov", ">= 0"
9
+ # gem "shoulda", ">= 0"
10
+ # gem "rdoc", "~> 3.12"
11
+ # gem "bundler", "~> 1.3.5"
12
+ gem "bundler", ">= 1.3.5"
13
+ # gem "jeweler", "~> 1.8.4"
14
+ gem "jeweler", ">= 1.8.4"
15
+ # gem "simplecov-rcov", ">= 0"
16
+ gem "test-unit", ">= 3.1.5"
14
17
  end
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 YAMAMOTO, Masayuki
1
+ Copyright (c) 2013- YAMAMOTO, Masayuki
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
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
@@ -3,6 +3,7 @@
3
3
  ## Description
4
4
 
5
5
  provides the methods to separate a number with the delimiter for each 3 digits.
6
+ * 漢数字を含む文字列を実際の数値に変換する `KanNum.str_to_num` (`String#str_to_num`) を追加
6
7
 
7
8
  ## Installation
8
9
 
@@ -12,12 +13,39 @@ provides the methods to separate a number with the delimiter for each 3 digits.
12
13
 
13
14
  ## Usage
14
15
 
15
- 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.
16
42
 
17
43
  ## Lisence
44
+
18
45
  MIT Lisence.
19
46
 
20
47
  ## About the Author
48
+
21
49
  YAMAMOTO, Masayuki
22
50
 
23
51
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.3.1
data/digit_delim.gemspec CHANGED
@@ -2,60 +2,53 @@
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.3.1 ruby lib
5
6
 
6
7
  Gem::Specification.new do |s|
7
- s.name = "digit_delim"
8
- s.version = "0.0.1"
8
+ s.name = "digit_delim".freeze
9
+ s.version = "0.3.1"
9
10
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["YAMAMOTO, Masayuki"]
12
- s.date = "2013-07-30"
13
- s.description = "methods of num-string formatter, converter which extends String class"
14
- s.email = "martin.route66.blues+github@gmail.com"
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib".freeze]
13
+ s.authors = ["YAMAMOTO, Masayuki".freeze]
14
+ s.date = "2021-07-13"
15
+ s.description = "methods of num-string formatter, converter which extends String class".freeze
16
+ s.email = "martin.route66.blues+github@gmail.com".freeze
15
17
  s.extra_rdoc_files = [
16
- "LICENSE.txt"
18
+ "LICENSE.txt",
19
+ "ReadMe.md"
17
20
  ]
18
21
  s.files = [
19
22
  ".document",
20
23
  "Gemfile",
21
- "Gemfile.lock",
22
24
  "LICENSE.txt",
23
25
  "Rakefile",
24
26
  "ReadMe.md",
25
27
  "VERSION",
26
28
  "digit_delim.gemspec",
27
29
  "lib/digit_delim.rb",
30
+ "lib/kannum.rb",
28
31
  "test/helper.rb",
29
- "test/test_digit_delim.rb"
32
+ "test/test_digit_delim.rb",
33
+ "test/test_kannum.rb"
30
34
  ]
31
- s.homepage = "http://github.com/mephistobooks/digit_delim"
32
- s.licenses = ["MIT"]
33
- s.require_paths = ["lib"]
34
- s.rubygems_version = "2.0.3"
35
- s.summary = "A little num-string formatter, converter."
35
+ s.homepage = "http://github.com/mephistobooks/digit_delim".freeze
36
+ s.licenses = ["MIT".freeze]
37
+ s.rubygems_version = "3.2.20".freeze
38
+ s.summary = "A little num-string formatter, converter.".freeze
36
39
 
37
40
  if s.respond_to? :specification_version then
38
41
  s.specification_version = 4
42
+ end
39
43
 
40
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
- s.add_development_dependency(%q<shoulda>, [">= 0"])
42
- s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
43
- s.add_development_dependency(%q<bundler>, ["~> 1.3.5"])
44
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
45
- s.add_development_dependency(%q<simplecov-rcov>, [">= 0"])
46
- else
47
- s.add_dependency(%q<shoulda>, [">= 0"])
48
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
49
- s.add_dependency(%q<bundler>, ["~> 1.3.5"])
50
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
51
- s.add_dependency(%q<simplecov-rcov>, [">= 0"])
52
- end
44
+ if s.respond_to? :add_runtime_dependency then
45
+ s.add_development_dependency(%q<bundler>.freeze, [">= 1.3.5"])
46
+ s.add_development_dependency(%q<jeweler>.freeze, [">= 1.8.4"])
47
+ s.add_development_dependency(%q<test-unit>.freeze, [">= 3.1.5"])
53
48
  else
54
- s.add_dependency(%q<shoulda>, [">= 0"])
55
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
56
- s.add_dependency(%q<bundler>, ["~> 1.3.5"])
57
- s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
58
- s.add_dependency(%q<simplecov-rcov>, [">= 0"])
49
+ s.add_dependency(%q<bundler>.freeze, [">= 1.3.5"])
50
+ s.add_dependency(%q<jeweler>.freeze, [">= 1.8.4"])
51
+ s.add_dependency(%q<test-unit>.freeze, [">= 3.1.5"])
59
52
  end
60
53
  end
61
54
 
data/lib/digit_delim.rb CHANGED
@@ -14,19 +14,31 @@ class String
14
14
  # with delimiters: separate digit-string into each n-digits
15
15
  # with delimiters.
16
16
  # ==== Args
17
- # n :: the number of digits length of separated string.
17
+ # adp :: length of digits after the decimal point.
18
+ # (-1 if you want all of digits after the decimal point)
19
+ # n :: the number of digits length of each separated string.
18
20
  # ==== Return
19
21
  # separated string.
20
- def w_dm(n=3); self.gsub( /(\d+)?(\.\d*)?/ ) { \
21
- s1="#{$1}".dup; s2="#{$2}".dup; \
22
- s2="#{s2}00".slice(0,s2.size) if s2.size != 0
23
- s1.reverse.gsub( /\d{#{n}}(?=\d)/ ){|r| r+","}.reverse+s2 }; end
22
+ def w_dm(adp=-1, n=3)
23
+ self.gsub( /(\d+)?(\.\d*)?/ ) {
24
+ s1="#{$1}".dup; s2="#{$2}".dup;
25
+
26
+ if s2.size != 0
27
+ if adp == -1
28
+ s2="#{s2}00".slice(0, s2.size)
29
+ else
30
+ s2="#{s2}00".slice(0, adp+1)
31
+ end
32
+ end
33
+ s1.reverse.gsub( /\d{#{n}}(?=\d)/ ){|r| r+","}.reverse+s2
34
+ }
35
+ end
24
36
  alias to_f_old to_f
25
37
 
26
38
  # :method: wo_dm
27
39
  # without delimiters: remove the delimiters in digit-string.
28
40
  # ==== Args
29
- #
41
+ # none.
30
42
  # ==== Return
31
43
  # digit-string without the delimiters.
32
44
  def wo_dm; self.gsub(/,/, ""); end
@@ -34,25 +46,35 @@ class String
34
46
  # :method: to_wof
35
47
  # remove delimiters in digit-string and convert it to Float.
36
48
  # ==== Args
37
- #
49
+ # none.
38
50
  # ==== Return
39
- # number in Float
51
+ # A number in Float.
40
52
  def to_wof; self.wo_dm.to_f_old; end
41
53
 
42
54
  # :method: to_fu
43
- # convert string to float concerning with unit.
55
+ # convert digit-string into float concerning with unit.
44
56
  # ==== Args
45
- #
57
+ # none.
46
58
  # ==== Return
47
- # number in Float. if the original string has '%', the number is
48
- # divided by 100.
59
+ # A number in Float. If the original string has '%', the number is
60
+ # divided by 100.0.
49
61
  def to_fu
50
62
  ret = self.to_wof
51
- if self =~ /%$/ then ret /= 100.0 else ret end
63
+ # if self =~ /%$/ then ret /= 100.0 else ret end
64
+ case self
65
+ when /%$/
66
+ ret /= 100.0
67
+
68
+ else
69
+ ret
70
+
71
+ end
72
+
52
73
  end
53
74
 
54
75
  end
55
76
 
56
77
 
78
+ require "kannum"
57
79
 
58
80
  #### endof filename: digit_delim.rb
data/lib/kannum.rb ADDED
@@ -0,0 +1,312 @@
1
+ #
2
+ #
3
+ #
4
+ #
5
+
6
+
7
+ module KanNum
8
+ N_数 = {
9
+ '〇'=>0,
10
+ '一'=>1, '壱'=>1,
11
+ '二'=>2, '弐'=>2,
12
+ '三'=>3, '参'=>3,
13
+ '四'=>4,
14
+ '五'=>5,
15
+ '六'=>6,
16
+ '七'=>7,
17
+ '八'=>8,
18
+ '九'=>9,
19
+
20
+ '0'=>0,
21
+ '1'=>1,
22
+ '2'=>2,
23
+ '3'=>3,
24
+ '4'=>4,
25
+ '5'=>5,
26
+ '6'=>6,
27
+ '7'=>7,
28
+ '8'=>8,
29
+ '9'=>9,
30
+
31
+ '−'=>'-', # 全角マイナス => '-'
32
+ '-'=>'-', # minus (dummy map)
33
+ }
34
+
35
+ N_数_ext = {
36
+ '零'=>0,
37
+ '肆'=>4,
38
+ '伍'=>5,
39
+ '陸'=>6,
40
+ '漆'=>7, '質'=>7,
41
+ '捌'=>8,
42
+ '玖'=>9,
43
+ }
44
+
45
+ N_倍数 = {
46
+ '十' => 10**1, '拾' => 10**1,
47
+ '百' => 10**2,
48
+ '千' => 10**3,
49
+ }
50
+
51
+ N_倍数_ext = {
52
+ '阡' => 10**3,
53
+ }
54
+
55
+ N_合成倍数 = {
56
+ '廿' => 2*10,
57
+ '丗' => 3*10,
58
+ '卌' => 4*10,
59
+ }
60
+
61
+ N_単位数 = {
62
+ '万' => 10**4, '萬' => 10**4,
63
+ '億' => 10**8,
64
+ '兆' => 10**12,
65
+ '京' => 10**16,
66
+ '垓' => 10**20,
67
+ }
68
+
69
+ N_ary = [ N_数, N_倍数, N_単位数, ]
70
+ N_ary_loose = N_ary + [ N_数_ext, N_倍数_ext, N_合成倍数, ]
71
+
72
+ N_keys = N_ary.map{|e| e.keys}.inject(:+)
73
+ N_keys_loose = N_ary_loose.map{|e| e.keys}.inject(:+)
74
+
75
+ #
76
+ #
77
+ #
78
+ def self.numable?( str, dict = N_keys )
79
+ tmp_str = str.gsub(/[0-90-9]/,'')
80
+ # tmp = dict.map{|e| e.keys}.inject(:+)
81
+ tmp = dict
82
+ #tmp += (0..9).to_a
83
+
84
+ ret = true
85
+ (0..(tmp_str.size-1)).each {|i|
86
+ c = tmp_str[i]
87
+ if tmp.include?(c)
88
+ next
89
+ else
90
+ ret = false
91
+ break
92
+ end
93
+ }
94
+ ret
95
+ end
96
+ def self.numable_loose?( str )
97
+ numable?(str, N_keys_loose)
98
+ end
99
+
100
+
101
+ # simple chr-to-chr translation: 0-9.
102
+ #
103
+ #
104
+ def self.translate_phase0( str )
105
+ #str = str.tr("0-9", "0-9")
106
+
107
+ tmp_str = str
108
+ # str.split(//).each_with_index{|e,i|
109
+ (0..(str.length-1)).each{|i|
110
+ c = str[i]
111
+ tmp = N_数[c]
112
+ tmp_str[i] = tmp.to_s unless tmp.nil?
113
+ }
114
+
115
+ tmp_str
116
+ end
117
+
118
+ RE_NUM_O = "-.0-9"
119
+ RE_NUM = "[#{RE_NUM_O}]+"
120
+ RE_NONNUM = "[^#{RE_NUM_O} ]"
121
+
122
+ # create parse tree for str:
123
+ # [ [[ num, Num, ... ], UnitNum], ... ]
124
+ #
125
+ def self.translate_phase1( str )
126
+ ret = []
127
+ tmp_ary = []
128
+ units = N_単位数.keys
129
+
130
+ # $stderr.puts "#{__method__} str: #{str}"
131
+ # str.split(//).each do |c|
132
+ (0..(str.length-1)).each do |i|
133
+ c = str[i]
134
+
135
+ if units.include?(c)
136
+ tmp = tmp_ary.join
137
+ # $stderr.puts "#{__method__} tmp: #{tmp}"
138
+ #tmp = tmp.gsub(/([0-9]+)([^ 0-9])/, '\1 \2')
139
+ tmp = tmp.gsub(/(#{RE_NUM})(#{RE_NONNUM})/, '\1 \2')
140
+ # $stderr.puts "#{__method__} tmp: #{tmp}"
141
+ #tmp = tmp.gsub(/([^ 0-9])([0-9]+)/, '\1 \2')
142
+ tmp = tmp.gsub(/(#{RE_NONNUM})(#{RE_NUM})/, '\1 \2')
143
+
144
+ #tmp = tmp.gsub(/([^ 0-9])([^ 0-9])/, '\1 \2')
145
+ tmp = tmp.gsub(/(#{RE_NONNUM})(#{RE_NONNUM})/, '\1 \2')
146
+ # $stderr.puts "#{__method__} tmp: #{tmp}"
147
+ tmp = tmp.split(/\s+/)
148
+ # $stderr.puts "#{__method__} tmp: #{tmp}"
149
+ ret << [tmp, c]
150
+ tmp_ary = []
151
+ next
152
+ end
153
+ tmp_ary << c
154
+ # $stderr.puts "#{__method__} tmp_ary: #{tmp_ary}"
155
+ end
156
+ ret << tmp_ary if tmp_ary.size > 0
157
+
158
+ ret
159
+ end
160
+
161
+ # calculate number due to parse tree.
162
+ # ==== Args
163
+ # ary :: parse tree with _phase1
164
+ # ==== Return
165
+ # number
166
+ def self.translate_phase2( ary )
167
+ ret = 0
168
+ # $stderr.puts "{#{__method__}} ary: #{ary}"
169
+
170
+ #
171
+ ary.each do |ua|
172
+ na, u = ua
173
+ if na.class != Array
174
+ na = ua
175
+ u = '1'
176
+ end
177
+
178
+ tmp = 0
179
+ tmp_ary = []
180
+
181
+ na.each do |e|
182
+ tmp_ary << e
183
+ if e =~ /[0-9]+/
184
+ else
185
+ # $stderr.puts "#{__method__} tmp_ary: #{tmp_ary}"
186
+ a = 1
187
+ b = N_倍数[tmp_ary[-1]]
188
+ #a = tmp_ary[0].to_i if tmp_ary.size > 1
189
+ a = tmp_ary[0].to_f if tmp_ary.size > 1
190
+
191
+ tmp += a*b
192
+ tmp_ary = []
193
+ end
194
+ end
195
+ # $stderr.puts "#{__method__} tmp_ary: #{tmp_ary}"
196
+ #tmp += tmp_ary[0].to_i if tmp_ary.size != 0
197
+ tmp += tmp_ary[0].to_f if tmp_ary.size != 0
198
+
199
+ tmp_u = N_単位数[u]
200
+ if tmp_u.nil?
201
+ ret += tmp * 1
202
+ else
203
+ ret += tmp * tmp_u
204
+ end
205
+
206
+ end
207
+
208
+ if ret - ret.to_i > 0.0
209
+ else
210
+ ret = ret.to_i
211
+ end
212
+
213
+ ret
214
+ end
215
+
216
+ def self.str_to_num( str )
217
+
218
+ minus_f = false
219
+ if str =~ /^-/
220
+ str = str.gsub(/^-/, '')
221
+ minus_f = true
222
+ end
223
+
224
+ tmp = translate_phase0(str)
225
+ # $stderr.puts "result0: #{tmp}"
226
+ tmp = translate_phase1(tmp)
227
+ # $stderr.puts "result1: #{tmp}"
228
+ ret = translate_phase2(tmp)
229
+
230
+ ret *= -1 if minus_f
231
+
232
+ ret
233
+ end
234
+
235
+
236
+ #
237
+ #
238
+ #
239
+ def self.convert_helper( num )
240
+
241
+ tmp = nil
242
+ N_単位数.each_pair do |k,v|
243
+ # $stderr.puts "#{__method__} #{k} #{v}"
244
+ if num >= v
245
+ tmp ||= k
246
+ tmp = k if not(tmp.nil?) and v > N_単位数[tmp]
247
+ # $stderr.puts "#{__method__} tmp: #{tmp}"
248
+ next
249
+ else
250
+ break
251
+ end
252
+ end
253
+
254
+ tmp
255
+ end
256
+
257
+ def self.convert_phase0( num )
258
+ ret = []
259
+
260
+ tmp_num = num
261
+ while tmp = convert_helper(tmp_num)
262
+
263
+ u = N_単位数[tmp]
264
+ r = tmp_num/u
265
+ ret << [r.to_i, tmp]
266
+ tmp_num -= r*u
267
+ end
268
+
269
+ ret << tmp_num if tmp_num != 0
270
+ ret
271
+ end
272
+
273
+ def self.convert_phase1( ary )
274
+ ary.flatten.join
275
+ end
276
+
277
+ def self.num_to_str( num )
278
+
279
+ minus_f = false
280
+ if num < 0
281
+ num *= -1
282
+ minus_f = true
283
+ end
284
+
285
+ tmp = convert_phase0(num)
286
+ # $stderr.puts "{#{__method__}}result0: #{tmp}"
287
+ ret = convert_phase1(tmp)
288
+ # $stderr.puts "result1: #{ret}"
289
+
290
+ if minus_f
291
+ ret = "-"+ret
292
+ end
293
+
294
+ ret
295
+ end
296
+
297
+ end
298
+
299
+
300
+ #
301
+ #
302
+ #
303
+ class String
304
+ def str_to_num; KanNum.str_to_num self; end
305
+ end
306
+
307
+ class Fixnum
308
+ def num_to_str; KanNum.num_to_str self; end
309
+ end
310
+
311
+
312
+ #### endof filename: .rb
data/test/helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'rubygems'
1
+ # require 'rubygems'
2
2
  require 'bundler'
3
3
  begin
4
4
  Bundler.setup(:default, :development)
@@ -8,7 +8,7 @@ rescue Bundler::BundlerError => e
8
8
  exit e.status_code
9
9
  end
10
10
  require 'test/unit'
11
- require 'shoulda'
11
+ #require 'shoulda'
12
12
 
13
13
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
14
  $LOAD_PATH.unshift(File.dirname(__FILE__))
@@ -1,7 +1,7 @@
1
1
  #!/usr/local/bin/ruby -w
2
2
 
3
3
  # filename: test_digit_delim.rb
4
- #
4
+ # To do this test case, ``rake test``.
5
5
  #
6
6
  #require "rubygems"
7
7
  #gem "test-unit"
@@ -9,7 +9,7 @@
9
9
  require "helper"
10
10
  require 'test/unit'
11
11
 
12
-
12
+ # Testee.
13
13
  require "digit_delim"
14
14
 
15
15
 
@@ -17,10 +17,15 @@ class TestDigitDelim < Test::Unit::TestCase
17
17
 
18
18
  def test_w_dm_1
19
19
  assert_equal( "123", "123".w_dm )
20
+ assert_equal( "1,000", 1000.to_s.w_dm )
21
+
20
22
  assert_equal( "23,456,789", "23456789".w_dm )
21
23
  assert_equal( "23,456,789.0123", "23456789.0123".w_dm )
22
- assert_equal( "23,456,789.0123", "23456789.0123".w_dm(3) )
23
- assert_equal( "2345,6789.0123", "23456789.0123".w_dm(4) )
24
+ assert_equal( "23,456,789.0123", "23456789.0123".w_dm(-1, 3) )
25
+ assert_equal( "2345,6789.0123", "23456789.0123".w_dm(-1, 4) )
26
+
27
+ assert_equal( "23,456,789.01", "23456789.0123".w_dm(2) )
28
+ assert_equal( "2345,6789.01", "23456789.0123".w_dm(2,4) )
24
29
  end
25
30
 
26
31
  def test_w_dm_2
@@ -34,21 +39,47 @@ class TestDigitDelim < Test::Unit::TestCase
34
39
  assert_equal( "12,345,678.01234", "12345678.01234".w_dm )
35
40
  end
36
41
 
42
+ def test_w_dm_3
43
+ t_pat = 23456789.0123
44
+
45
+ assert_equal( "2345,6789.01", t_pat.to_s.w_dm(2, 4) )
46
+ assert_equal( "2,3,4,5,6,7,8,9.01", t_pat.to_s.w_dm(2, 1) )
47
+
48
+ # with sign.
49
+ assert_equal( "+2,100", sprintf("%+d", 2_100).w_dm )
50
+ assert_equal( "-2,100", sprintf("%+d", -2_100).w_dm )
51
+
52
+ # with sign, in percentage (符号付き, パーセント表記)
53
+ assert_equal( "-10.10%", sprintf("%+.2f%%", -0.1010*100).w_dm )
54
+ assert_equal( "+10.10%", sprintf("%+.2f%%", +0.1010*100).w_dm )
55
+
56
+ end
57
+
37
58
  def test_wo_dm
38
59
  assert_equal( "100000", "100,000".wo_dm )
60
+ assert_equal( "100000", "100,000".wo_dm.to_i.to_s )
39
61
  assert_equal( "100000.0123", "100,000.0123".wo_dm )
40
62
  end
41
63
 
42
64
  def test_to_wof
43
- assert_equal( 100, "100,000".to_f )
44
- assert_equal( 100000, "100,000".to_wof )
65
+ exp = 100_000
45
66
 
67
+ assert_not_equal( exp, "100,000".to_f )
46
68
  assert_equal( 100.0, "100,000".to_f )
69
+
70
+ assert_equal( exp, "100,000".to_wof )
71
+ assert_not_equal( exp.to_s, "100,000".to_wof.to_s )
72
+ assert_equal( exp.to_s + ".0", "100,000".to_wof.to_s )
73
+
74
+ #
75
+ assert_equal( 1791.11, "1,791.11".to_wof )
47
76
  end
48
77
 
49
78
  def test_to_wof_stocks
50
79
  assert_equal( 1, "1株".to_wof )
51
80
  assert_equal( 1.05, "1.05株".to_wof )
81
+
82
+ assert_equal( 10, "10百万円".to_wof )
52
83
  end
53
84
 
54
85
  def test_to_fu
@@ -0,0 +1,185 @@
1
+ #
2
+ # filename: test_kannum.rb
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
+ #
12
+ #
13
+ require "helper"
14
+ require 'test/unit'
15
+
16
+ # Testee.
17
+ require "digit_delim"
18
+
19
+
20
+ class TestKanNum < Test::Unit::TestCase
21
+
22
+ def setup
23
+
24
+ @pattern_t1 = "12億3456万"
25
+ @pattern_n1 = 12_3456_0000
26
+
27
+ @pattern_t2 = "100億"
28
+ @pattern_n2 = 100_0000_0000
29
+ @pattern_t21 = "百億"
30
+ @pattern_t22 = "100億"
31
+
32
+ @pattern_t3 = "2千億3千4百十万"
33
+ @pattern_n3 = 2000_3410_0000
34
+ @pattern_t31 = "2千億3千十万"
35
+ @pattern_n31 = 2000_3010_0000
36
+ @pattern_t32 = "2千億千十万"
37
+ @pattern_n32 = 2000_1010_0000
38
+
39
+ @pattern_t4 = "千百3十万"
40
+ @pattern_n4 = 1030_0000
41
+ @pattern_t5 = "参拾"
42
+ @pattern_n5 = 30
43
+ @pattern_t6 = "参"
44
+ @pattern_n6 = 3
45
+ @pattern_t7 = "拾"
46
+ @pattern_n7 = 10
47
+
48
+ end
49
+
50
+ def tear
51
+ end
52
+
53
+ def test_basic
54
+
55
+ ret = "壱萬参千弐百".str_to_num
56
+ exp = 1_3200
57
+ assert_equal( exp, ret )
58
+
59
+ ret = "-3億900万".str_to_num
60
+ exp = -3_0900_0000
61
+ assert_equal( exp, ret )
62
+
63
+ ret = @pattern_t1.str_to_num
64
+ exp = @pattern_n1
65
+ assert_equal( exp, ret )
66
+
67
+ ret = KanNum.str_to_num @pattern_t2
68
+ exp = @pattern_n2
69
+ assert_equal( exp, ret )
70
+
71
+ ret = KanNum.str_to_num @pattern_t21
72
+ exp = @pattern_n2
73
+ assert_equal( exp, ret )
74
+
75
+ ret = KanNum.str_to_num @pattern_t22
76
+ exp = @pattern_n2
77
+ assert_equal( exp, ret )
78
+
79
+ ret = KanNum.str_to_num @pattern_t3
80
+ exp = @pattern_n3
81
+ assert_equal( exp, ret )
82
+
83
+ ret = KanNum.str_to_num @pattern_t31
84
+ exp = @pattern_n31
85
+ assert_equal( exp, ret )
86
+
87
+ ret = KanNum.str_to_num @pattern_t32
88
+ exp = @pattern_n32
89
+ assert_equal( exp, ret )
90
+
91
+ ret = KanNum.str_to_num @pattern_t4
92
+ exp = 1130_0000
93
+ assert_equal( exp, ret )
94
+
95
+ ret = KanNum.str_to_num @pattern_t5
96
+ exp = 30
97
+ assert_equal( exp, ret )
98
+
99
+ ret = KanNum.str_to_num @pattern_t6
100
+ exp = 3
101
+ assert_equal( exp, ret )
102
+
103
+ ret = KanNum.str_to_num @pattern_t7
104
+ exp = 10
105
+ assert_equal( exp, ret )
106
+
107
+ end
108
+
109
+ def test_str_to_num
110
+
111
+ ret = "0.7億".str_to_num
112
+ exp = 7000_0000
113
+ assert_equal( exp, ret )
114
+
115
+ ret = "-5.2億".str_to_num
116
+ exp = -5_2000_0000
117
+ assert_equal( exp, ret )
118
+
119
+ end
120
+
121
+ def test_numable?
122
+
123
+ ret = KanNum.numable? @pattern_t1
124
+ exp = true
125
+ assert_equal( exp, ret )
126
+
127
+ ret = KanNum.numable? "数字ではない文字をふくむ1つの例"
128
+ exp = false
129
+ assert_equal( exp, ret )
130
+
131
+ end
132
+
133
+ def test_num_to_str
134
+
135
+ #
136
+ ret = 1_3200.num_to_str
137
+ exp = "1万3200"
138
+ assert_equal( exp, ret )
139
+
140
+ ret = -3_0900_0000.num_to_str
141
+ exp = "-3億900万"
142
+ assert_equal( exp, ret )
143
+
144
+ ret = @pattern_n1.num_to_str
145
+ exp = @pattern_t1
146
+ assert_equal( exp, ret )
147
+
148
+ ret = KanNum.num_to_str @pattern_n2
149
+ exp = @pattern_t2
150
+ assert_equal( exp, ret )
151
+
152
+ ret = KanNum.num_to_str @pattern_n3
153
+ exp = "2000億3410万"
154
+ assert_equal( exp, ret )
155
+
156
+ ret = @pattern_n31.num_to_str
157
+ exp = "2000億3010万"
158
+ assert_equal( exp, ret )
159
+
160
+ ret = @pattern_n32.num_to_str
161
+ exp = "2000億1010万"
162
+ assert_equal( exp, ret )
163
+
164
+ ret = @pattern_n4.num_to_str
165
+ exp = "1030万"
166
+ assert_equal( exp, ret )
167
+
168
+ ret = @pattern_n5.num_to_str
169
+ exp = "30"
170
+ assert_equal( exp, ret )
171
+
172
+ ret = @pattern_n6.num_to_str
173
+ exp = "3"
174
+ assert_equal( exp, ret )
175
+
176
+ ret = @pattern_n7.num_to_str
177
+ exp = "10"
178
+ assert_equal( exp, ret )
179
+
180
+ end
181
+
182
+ end
183
+
184
+
185
+ #### endof filename: test_kannum.rb
metadata CHANGED
@@ -1,103 +1,77 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: digit_delim
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - YAMAMOTO, Masayuki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-31 00:00:00.000000000 Z
11
+ date: 2021-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: shoulda
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - '>='
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '>='
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rdoc
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ~>
32
- - !ruby/object:Gem::Version
33
- version: '3.12'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ~>
39
- - !ruby/object:Gem::Version
40
- version: '3.12'
41
13
  - !ruby/object:Gem::Dependency
42
14
  name: bundler
43
15
  requirement: !ruby/object:Gem::Requirement
44
16
  requirements:
45
- - - ~>
17
+ - - ">="
46
18
  - !ruby/object:Gem::Version
47
19
  version: 1.3.5
48
20
  type: :development
49
21
  prerelease: false
50
22
  version_requirements: !ruby/object:Gem::Requirement
51
23
  requirements:
52
- - - ~>
24
+ - - ">="
53
25
  - !ruby/object:Gem::Version
54
26
  version: 1.3.5
55
27
  - !ruby/object:Gem::Dependency
56
28
  name: jeweler
57
29
  requirement: !ruby/object:Gem::Requirement
58
30
  requirements:
59
- - - ~>
31
+ - - ">="
60
32
  - !ruby/object:Gem::Version
61
33
  version: 1.8.4
62
34
  type: :development
63
35
  prerelease: false
64
36
  version_requirements: !ruby/object:Gem::Requirement
65
37
  requirements:
66
- - - ~>
38
+ - - ">="
67
39
  - !ruby/object:Gem::Version
68
40
  version: 1.8.4
69
41
  - !ruby/object:Gem::Dependency
70
- name: simplecov-rcov
42
+ name: test-unit
71
43
  requirement: !ruby/object:Gem::Requirement
72
44
  requirements:
73
- - - '>='
45
+ - - ">="
74
46
  - !ruby/object:Gem::Version
75
- version: '0'
47
+ version: 3.1.5
76
48
  type: :development
77
49
  prerelease: false
78
50
  version_requirements: !ruby/object:Gem::Requirement
79
51
  requirements:
80
- - - '>='
52
+ - - ">="
81
53
  - !ruby/object:Gem::Version
82
- version: '0'
54
+ version: 3.1.5
83
55
  description: methods of num-string formatter, converter which extends String class
84
56
  email: martin.route66.blues+github@gmail.com
85
57
  executables: []
86
58
  extensions: []
87
59
  extra_rdoc_files:
88
60
  - LICENSE.txt
61
+ - ReadMe.md
89
62
  files:
90
- - .document
63
+ - ".document"
91
64
  - Gemfile
92
- - Gemfile.lock
93
65
  - LICENSE.txt
94
66
  - Rakefile
95
67
  - ReadMe.md
96
68
  - VERSION
97
69
  - digit_delim.gemspec
98
70
  - lib/digit_delim.rb
71
+ - lib/kannum.rb
99
72
  - test/helper.rb
100
73
  - test/test_digit_delim.rb
74
+ - test/test_kannum.rb
101
75
  homepage: http://github.com/mephistobooks/digit_delim
102
76
  licenses:
103
77
  - MIT
@@ -108,17 +82,16 @@ require_paths:
108
82
  - lib
109
83
  required_ruby_version: !ruby/object:Gem::Requirement
110
84
  requirements:
111
- - - '>='
85
+ - - ">="
112
86
  - !ruby/object:Gem::Version
113
87
  version: '0'
114
88
  required_rubygems_version: !ruby/object:Gem::Requirement
115
89
  requirements:
116
- - - '>='
90
+ - - ">="
117
91
  - !ruby/object:Gem::Version
118
92
  version: '0'
119
93
  requirements: []
120
- rubyforge_project:
121
- rubygems_version: 2.0.6
94
+ rubygems_version: 3.2.20
122
95
  signing_key:
123
96
  specification_version: 4
124
97
  summary: A little num-string formatter, converter.
data/Gemfile.lock DELETED
@@ -1,79 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- activesupport (4.0.0)
5
- i18n (~> 0.6, >= 0.6.4)
6
- minitest (~> 4.2)
7
- multi_json (~> 1.3)
8
- thread_safe (~> 0.1)
9
- tzinfo (~> 0.3.37)
10
- addressable (2.3.5)
11
- atomic (1.1.10)
12
- builder (3.2.2)
13
- faraday (0.8.8)
14
- multipart-post (~> 1.2.0)
15
- git (1.2.5)
16
- github_api (0.10.1)
17
- addressable
18
- faraday (~> 0.8.1)
19
- hashie (>= 1.2)
20
- multi_json (~> 1.4)
21
- nokogiri (~> 1.5.2)
22
- oauth2
23
- hashie (2.0.5)
24
- highline (1.6.19)
25
- httpauth (0.2.0)
26
- i18n (0.6.4)
27
- jeweler (1.8.6)
28
- builder
29
- bundler (~> 1.0)
30
- git (>= 1.2.5)
31
- github_api (= 0.10.1)
32
- highline (>= 1.6.15)
33
- nokogiri (= 1.5.10)
34
- rake
35
- rdoc
36
- json (1.8.0)
37
- jwt (0.1.8)
38
- multi_json (>= 1.5)
39
- minitest (4.7.5)
40
- multi_json (1.7.7)
41
- multi_xml (0.5.4)
42
- multipart-post (1.2.0)
43
- nokogiri (1.5.10)
44
- oauth2 (0.9.2)
45
- faraday (~> 0.8)
46
- httpauth (~> 0.2)
47
- jwt (~> 0.1.4)
48
- multi_json (~> 1.0)
49
- multi_xml (~> 0.5)
50
- rack (~> 1.2)
51
- rack (1.5.2)
52
- rake (10.1.0)
53
- rdoc (3.12.2)
54
- json (~> 1.4)
55
- shoulda (3.5.0)
56
- shoulda-context (~> 1.0, >= 1.0.1)
57
- shoulda-matchers (>= 1.4.1, < 3.0)
58
- shoulda-context (1.1.4)
59
- shoulda-matchers (2.2.0)
60
- activesupport (>= 3.0.0)
61
- simplecov (0.7.1)
62
- multi_json (~> 1.0)
63
- simplecov-html (~> 0.7.1)
64
- simplecov-html (0.7.1)
65
- simplecov-rcov (0.2.3)
66
- simplecov (>= 0.4.1)
67
- thread_safe (0.1.2)
68
- atomic
69
- tzinfo (0.3.37)
70
-
71
- PLATFORMS
72
- ruby
73
-
74
- DEPENDENCIES
75
- bundler (~> 1.3.5)
76
- jeweler (~> 1.8.4)
77
- rdoc (~> 3.12)
78
- shoulda
79
- simplecov-rcov