si 0.1.3 → 0.1.4
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.
- data/lib/si/constants.rb +19 -1
- data/lib/si/module.rb +15 -3
- data/lib/si/version.rb +1 -1
- data/test/test_si.rb +2 -1
- metadata +3 -2
data/lib/si/constants.rb
CHANGED
@@ -1,7 +1,25 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module SI
|
4
|
-
PREFIXES =
|
4
|
+
PREFIXES = {
|
5
|
+
-8 => 'y',
|
6
|
+
-7 => 'z',
|
7
|
+
-6 => 'a',
|
8
|
+
-5 => 'f',
|
9
|
+
-4 => 'p',
|
10
|
+
-3 => 'n',
|
11
|
+
-2 => 'μ',
|
12
|
+
-1 => 'm',
|
13
|
+
0 => '',
|
14
|
+
1 => 'k',
|
15
|
+
2 => 'M',
|
16
|
+
3 => 'G',
|
17
|
+
4 => 'T',
|
18
|
+
5 => 'P',
|
19
|
+
6 => 'E',
|
20
|
+
7 => 'Z',
|
21
|
+
8 => 'Y'
|
22
|
+
}
|
5
23
|
|
6
24
|
DEFAULT = {
|
7
25
|
:length => 3,
|
data/lib/si/module.rb
CHANGED
@@ -17,7 +17,7 @@ module SI
|
|
17
17
|
denom = base ** exp
|
18
18
|
if nump >= denom || exp == min_exp
|
19
19
|
val = nump / denom
|
20
|
-
val =
|
20
|
+
val = SI.round val, [length - val.to_i.to_s.length, 0].max
|
21
21
|
val = val.to_i if exp == 0 && num.is_a?(Fixnum)
|
22
22
|
val = val.to_s.ljust(length + 1, '0') if val.is_a?(Float)
|
23
23
|
|
@@ -29,8 +29,8 @@ module SI
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def revert str, options = {}
|
32
|
-
options = DEFAULT.select { |k, v| k == :base }.merge(options)
|
33
|
-
pair = PREFIXES.to_a.find { |k, v| v
|
32
|
+
options = Hash[ DEFAULT.select { |k, v| k == :base } ].merge(options)
|
33
|
+
pair = PREFIXES.to_a.find { |k, v| !v.empty? && str =~ /[0-9]#{v}$/ }
|
34
34
|
|
35
35
|
if pair
|
36
36
|
str[0...-1].to_f * (options[:base] ** pair.first)
|
@@ -38,6 +38,18 @@ module SI
|
|
38
38
|
str.to_f
|
39
39
|
end
|
40
40
|
end
|
41
|
+
|
42
|
+
if (RUBY_VERSION.split('.')[0, 2].map(&:to_i) <=> [1, 8]) == 1
|
43
|
+
def round val, ndigits
|
44
|
+
val.round ndigits
|
45
|
+
end
|
46
|
+
else
|
47
|
+
def round val, ndigits
|
48
|
+
exp = (10 ** ndigits).to_f
|
49
|
+
val = ((val * exp).round / exp)
|
50
|
+
ndigits == 0 ? val.to_i : val
|
51
|
+
end
|
52
|
+
end
|
41
53
|
end
|
42
54
|
|
43
55
|
def si options = {}
|
data/lib/si/version.rb
CHANGED
data/test/test_si.rb
CHANGED
@@ -187,9 +187,10 @@ class TestSI < Test::Unit::TestCase
|
|
187
187
|
assert_equal 9876500000, SI.revert('9.8765G')
|
188
188
|
assert_equal 9876500, SI.revert('9.8765M')
|
189
189
|
assert_equal 9.8765 * 1024 ** 2, SI.revert('9.8765M', :base => 1024)
|
190
|
-
assert_equal 0.
|
190
|
+
assert_equal 0.0000098765, SI.revert('9.8765μ')
|
191
191
|
assert_equal 0.0098765, SI.revert('9.8765m')
|
192
192
|
assert_equal 9.8765, SI.revert('9.8765')
|
193
|
+
assert_equal 9.8765, SI.revert('9.8765xxxyyyzzz')
|
193
194
|
assert_equal 0.0, SI.revert('hello') # FIXME
|
194
195
|
end
|
195
196
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: si
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-03 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Formats a number with SI prefix
|
15
15
|
email:
|
@@ -57,3 +57,4 @@ specification_version: 3
|
|
57
57
|
summary: Formats a number with SI prefix (metric prefix)
|
58
58
|
test_files:
|
59
59
|
- test/test_si.rb
|
60
|
+
has_rdoc:
|