si 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -0
- data/lib/si/module.rb +4 -2
- data/lib/si/version.rb +1 -1
- data/test/test_si.rb +14 -0
- metadata +2 -2
data/README.md
CHANGED
data/lib/si/module.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
module SI
|
2
2
|
class << self
|
3
3
|
def convert num, options = {}
|
4
|
-
options = { :length => options } if options.is_a?(Fixnum)
|
4
|
+
options = { :length => options } if options.is_a?(Fixnum)
|
5
5
|
options = DEFAULT.merge(options)
|
6
6
|
length,
|
7
7
|
min_exp,
|
8
8
|
max_exp = options.values_at(:length, :min_exp, :max_exp)
|
9
|
+
raise ArgumentError.new("Invalid length") if length < 2
|
10
|
+
return num.is_a?(Fixnum) ? '0' : "0.#{'0' * (length - 1)}" if num == 0
|
11
|
+
|
9
12
|
base = options[:base].to_f
|
10
13
|
minus = num < 0 ? '-' : ''
|
11
14
|
nump = num.abs
|
@@ -27,7 +30,6 @@ module SI
|
|
27
30
|
|
28
31
|
def revert str, options = {}
|
29
32
|
options = DEFAULT.select { |k, v| k == :base }.merge(options)
|
30
|
-
base = options[:base]
|
31
33
|
pair = PREFIXES.to_a.find { |k, v| v == str[-1] }
|
32
34
|
|
33
35
|
if pair
|
data/lib/si/version.rb
CHANGED
data/test/test_si.rb
CHANGED
@@ -196,5 +196,19 @@ class TestSI < Test::Unit::TestCase
|
|
196
196
|
def test_rational
|
197
197
|
assert_equal '12.345n', (12345 * (10 ** -12)).si(5)
|
198
198
|
end
|
199
|
+
|
200
|
+
def test_zero
|
201
|
+
assert_equal '0.0000', 0.0.si(5)
|
202
|
+
assert_equal '0.000', 0.0.si(4)
|
203
|
+
assert_equal '0.0', 0.0.si(2)
|
204
|
+
assert_equal '0', 0.si(4)
|
205
|
+
assert_equal '0', 0.si(2)
|
206
|
+
assert_equal '0', 0.si
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_invalid_length
|
210
|
+
assert_raise(ArgumentError) { 123.si(0) }
|
211
|
+
assert_raise(ArgumentError) { 123.si(1) }
|
212
|
+
end
|
199
213
|
end
|
200
214
|
|
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.3
|
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-09-
|
12
|
+
date: 2012-09-14 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Formats a number with SI prefix
|
15
15
|
email:
|