spruz 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/spruz/round.rb +20 -9
- data/lib/spruz/version.rb +1 -1
- data/tests/test_spruz.rb +7 -3
- metadata +12 -5
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/spruz/round.rb
CHANGED
@@ -5,6 +5,7 @@ module Spruz
|
|
5
5
|
if klass.instance_method(:round)
|
6
6
|
klass.class_eval do
|
7
7
|
begin
|
8
|
+
alias_method :__old_round__, :round
|
8
9
|
remove_method :round
|
9
10
|
rescue NameError
|
10
11
|
end
|
@@ -15,9 +16,13 @@ module Spruz
|
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
18
|
-
def round(places =
|
19
|
-
|
20
|
-
|
19
|
+
def round(places = nil)
|
20
|
+
if places == nil || places == 0
|
21
|
+
return __old_round__
|
22
|
+
elsif places.respond_to?(:to_int)
|
23
|
+
places = places.to_int
|
24
|
+
else
|
25
|
+
raise TypeError, "argument places has to be like an Integer"
|
21
26
|
end
|
22
27
|
if places < 0
|
23
28
|
max_places = -Math.log(self.abs + 1) / Math.log(10)
|
@@ -26,13 +31,19 @@ module Spruz
|
|
26
31
|
t = self
|
27
32
|
f = 10.0 ** places
|
28
33
|
t *= f
|
29
|
-
if t
|
30
|
-
|
31
|
-
|
32
|
-
|
34
|
+
if t.infinite?
|
35
|
+
result = self
|
36
|
+
else
|
37
|
+
if t >= 0.0
|
38
|
+
t = (t + 0.5).floor
|
39
|
+
elsif t < 0.0
|
40
|
+
t = (t - 0.5).ceil
|
41
|
+
end
|
42
|
+
t /= f
|
43
|
+
result = t.nan? ? self : t
|
33
44
|
end
|
34
|
-
|
35
|
-
|
45
|
+
max_places and result = result.to_i # if places < 0
|
46
|
+
result
|
36
47
|
end
|
37
48
|
end
|
38
49
|
end
|
data/lib/spruz/version.rb
CHANGED
data/tests/test_spruz.rb
CHANGED
@@ -131,21 +131,25 @@ module Spruz
|
|
131
131
|
assert_equal(1, 1.round)
|
132
132
|
assert_equal(-1, -1.round)
|
133
133
|
assert_equal(2, 1.5.round)
|
134
|
+
assert_kind_of Integer, 1.5.round
|
134
135
|
assert_equal(-1, -1.4.round)
|
135
136
|
assert_equal(-2, -1.5.round)
|
136
137
|
end
|
137
138
|
|
138
139
|
def test_inclusion
|
139
140
|
assert_equal(10, 12.round(-1))
|
141
|
+
assert_kind_of Integer, 12.round(-1)
|
140
142
|
assert_equal(-10, -12.round(-1))
|
141
143
|
assert_raises(ArgumentError) { 12.round(-2) }
|
142
144
|
assert_raises(ArgumentError) { -12.round(-2) }
|
143
|
-
|
145
|
+
assert_in_delta(1.6, 1.55.round(1), 1E-1)
|
146
|
+
assert_kind_of Float, 1.55.round(1)
|
144
147
|
assert_equal(2, 1.55.round(0))
|
145
|
-
|
148
|
+
assert_in_delta(-1.5, -1.45.round(1), 1E-1)
|
146
149
|
assert_equal(-1, -1.45.round(0))
|
147
|
-
|
150
|
+
assert_in_delta(-1.6, -1.55.round(1), 1E-1)
|
148
151
|
assert_equal(-2, -1.55.round(0))
|
152
|
+
assert_in_delta(-1.55, -1.55.round(999), 1E-2)
|
149
153
|
end
|
150
154
|
end
|
151
155
|
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spruz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Florian Frank
|
@@ -9,7 +14,7 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-05-18 00:00:00 +02:00
|
13
18
|
default_executable:
|
14
19
|
dependencies: []
|
15
20
|
|
@@ -80,18 +85,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
80
85
|
requirements:
|
81
86
|
- - ">="
|
82
87
|
- !ruby/object:Gem::Version
|
88
|
+
segments:
|
89
|
+
- 0
|
83
90
|
version: "0"
|
84
|
-
version:
|
85
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
92
|
requirements:
|
87
93
|
- - ">="
|
88
94
|
- !ruby/object:Gem::Version
|
95
|
+
segments:
|
96
|
+
- 0
|
89
97
|
version: "0"
|
90
|
-
version:
|
91
98
|
requirements: []
|
92
99
|
|
93
100
|
rubyforge_project:
|
94
|
-
rubygems_version: 1.3.
|
101
|
+
rubygems_version: 1.3.6
|
95
102
|
signing_key:
|
96
103
|
specification_version: 3
|
97
104
|
summary: Useful stuff.
|