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 CHANGED
@@ -23,6 +23,8 @@ Or install it yourself as:
23
23
  Express a numeric value with SI prefix.
24
24
 
25
25
  ```ruby
26
+ require 'si'
27
+
26
28
  0.9.si # '900m'
27
29
  9.si # '9'
28
30
  98.si # '98'
@@ -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) && options >= 3
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
@@ -1,3 +1,3 @@
1
1
  module SI
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -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.2
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-13 00:00:00.000000000 Z
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: