digit_delim 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e50f4a154d5c40562dfebe807077312afc745822
4
- data.tar.gz: 9bf8033451bba472ca8b84d282ab070348e7feae
3
+ metadata.gz: 4b88f14aaa23708b6d5871ddb0091dd254c2a8f8
4
+ data.tar.gz: 176900dcc5f5fb58536a68995a4d31ad6a9a012d
5
5
  SHA512:
6
- metadata.gz: 6683d34ae966505dc8b27d786e6beb54ad9a637ea4c8e9e39fdecd0789679f24c0a7938d97521c398a2fc798f9dc9b1ea54d06db5a48aaa7145ac6f294e9cae4
7
- data.tar.gz: 329f504f02f0683473a5e08e073f2e22bf2305a45a677685909a48de91fb3eec2c337ebf71436ea8325cd55f4e78dc5ef3215af3a235036fa4f340263c0012b4
6
+ metadata.gz: a37abc2c0762fb0971fb90d6493e2df862b24972e5dba747bfb26d45ffe07a9b19ca02ebcd93d40273b1bcfa5abc838ad3515cabfa608f8b2baa42df5bbcfaa5
7
+ data.tar.gz: 3dfede9e812655f7485256d3c0723651aec96759ef9ffe92ab4ebcbdbaa7684c93d444e965bfe77b2fa5393fb1ccb16443b715c4aaff17cb60ece0eab9e8d64f
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -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,21 +46,30 @@ 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
@@ -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
 
@@ -19,8 +19,11 @@ class TestDigitDelim < Test::Unit::TestCase
19
19
  assert_equal( "123", "123".w_dm )
20
20
  assert_equal( "23,456,789", "23456789".w_dm )
21
21
  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) )
22
+ assert_equal( "23,456,789.0123", "23456789.0123".w_dm(-1, 3) )
23
+ assert_equal( "2345,6789.0123", "23456789.0123".w_dm(-1, 4) )
24
+
25
+ assert_equal( "23,456,789.01", "23456789.0123".w_dm(2) )
26
+ assert_equal( "2345,6789.01", "23456789.0123".w_dm(2,4) )
24
27
  end
25
28
 
26
29
  def test_w_dm_2
@@ -34,21 +37,35 @@ class TestDigitDelim < Test::Unit::TestCase
34
37
  assert_equal( "12,345,678.01234", "12345678.01234".w_dm )
35
38
  end
36
39
 
40
+ def test_w_dm_3
41
+ t_pat = 23456789.0123
42
+
43
+ assert_equal( "2345,6789.01", t_pat.to_s.w_dm(2, 4) )
44
+ assert_equal( "2,3,4,5,6,7,8,9.01", t_pat.to_s.w_dm(2, 1) )
45
+ end
46
+
37
47
  def test_wo_dm
38
48
  assert_equal( "100000", "100,000".wo_dm )
39
49
  assert_equal( "100000.0123", "100,000.0123".wo_dm )
40
50
  end
41
51
 
42
52
  def test_to_wof
43
- assert_equal( 100, "100,000".to_f )
44
- assert_equal( 100000, "100,000".to_wof )
53
+ exp = 100_000
45
54
 
55
+ assert_not_equal( exp, "100,000".to_f )
46
56
  assert_equal( 100.0, "100,000".to_f )
57
+
58
+ assert_equal( exp, "100,000".to_wof )
59
+ assert_not_equal( exp.to_s, "100,000".to_wof.to_s )
60
+ assert_equal( exp.to_s + ".0", "100,000".to_wof.to_s )
61
+
47
62
  end
48
63
 
49
64
  def test_to_wof_stocks
50
65
  assert_equal( 1, "1株".to_wof )
51
66
  assert_equal( 1.05, "1.05株".to_wof )
67
+
68
+ assert_equal( 10, "10百万円".to_wof )
52
69
  end
53
70
 
54
71
  def test_to_fu
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.0.1
4
+ version: 0.0.2
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: 2013-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shoulda
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  version: '0'
119
119
  requirements: []
120
120
  rubyforge_project:
121
- rubygems_version: 2.0.6
121
+ rubygems_version: 2.1.9
122
122
  signing_key:
123
123
  specification_version: 4
124
124
  summary: A little num-string formatter, converter.