sixarm_ruby_numeric_round 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # SixArm.com » Ruby » <br> Numeric rounding methods
1
+ # SixArm.com » Ruby » <br> Numeric #round, #floor, #ceil methods with precision
2
2
 
3
3
  * Doc: <http://sixarm.com/sixarm_ruby_numeric_round/doc>
4
4
  * Gem: <http://rubygems.org/gems/sixarm_ruby_numeric_round>
@@ -50,14 +50,16 @@ Round at a given decimal point:
50
50
  4.555.ceil_at(1) #=> 4.6
51
51
  4.555.floor_at(1) #=> 4.5
52
52
  4.555.round_at(1) #=> 4.6
53
+ 4.555.truncate_at(1) #=> 4.5
53
54
 
54
55
  Round to a given precision:
55
56
 
56
57
  4.555.ceil_to(0.1) #=> 4.6
57
58
  4.555.floor_to(0.1) #=> 4.5
58
59
  4.555.round_to(0.1) #=> 4.6
60
+ 4.555.truncate_to(0.1) #=> 4.5
59
61
 
60
- Conceptually, the methods for round, floor, and ceil will typically be for floating point numbers.
62
+ Conceptually, the methods for round, floor, ceil, and truncate will typically be for floating point numbers.
61
63
  However, these methods can actually be for pretty much any Numeric object.
62
64
  For example, one could round an Integer to the nearest kilo.
63
65
 
@@ -73,6 +75,7 @@ We're asking the Facets team to consider including the rest of the methods.
73
75
 
74
76
  ## Changes
75
77
 
78
+ * 2012-09-01 1.0.1 Add #truncate_at and #truncate_to.
76
79
  * 2012-05-30 1.0.0 Create baesd on Facets methods by Trans.
77
80
 
78
81
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
@@ -53,6 +53,22 @@ class Numeric
53
53
  to_f.round_to(*args)
54
54
  end
55
55
 
56
+ # Conceptually, truncate is expected to apply to floating point numbers.
57
+ # However it can actually be applied to pretty much any Numeric object.
58
+ # For example, one could truncate an Integer to the nearest kilo.
59
+ #
60
+ # See Float#truncate_at.
61
+
62
+ def truncate_at(*args)
63
+ to_f.truncate_at(*args)
64
+ end
65
+
66
+ # See Float#truncate_to.
67
+
68
+ def truncate_to(*args)
69
+ to_f.truncate_to(*args)
70
+ end
71
+
56
72
  end
57
73
 
58
74
  class Float
@@ -138,4 +154,31 @@ class Float
138
154
  (self * (1.0 / n)).round.to_f / (1.0 / n)
139
155
  end
140
156
 
157
+ # Truncate to the given decimal position.
158
+ #
159
+ # 4.555.truncate_at(0) #=> 4.0
160
+ # 4.555.truncate_at(1) #=> 4.5
161
+ # 4.555.truncate_at(2) #=> 4.55
162
+ # 4.555.truncate_at(3) #=> 4.555
163
+ #
164
+ # CREDIT: Trans & Joel Parker Henderson
165
+
166
+ def truncate_at( d ) #d=0
167
+ (self * (10.0 ** d)).truncate.to_f / (10.0 ** d)
168
+ end
169
+
170
+ # Truncates to the nearest _n_th degree.
171
+ #
172
+ # 4.555.truncate_to(1) #=> 4.0
173
+ # 4.555.truncate_to(0.1) #=> 4.5
174
+ # 4.555.truncate_to(0.01) #=> 4.55
175
+ # 4.555.truncate_to(0) #=> 4.555
176
+ #
177
+ # CREDIT: Trans & Joel Parker Henderson
178
+
179
+ def truncate_to( n ) #n=1
180
+ return self if n == 0
181
+ (self * (1.0 / n)).truncate.to_f / (1.0 / n)
182
+ end
183
+
141
184
  end
@@ -72,6 +72,28 @@ describe Float do
72
72
 
73
73
  end
74
74
 
75
+ describe "#truncate_at" do
76
+
77
+ it "works" do
78
+ 4.555.truncate_at(0).must_equal 4.0
79
+ 4.555.truncate_at(1).must_equal 4.5
80
+ 4.555.truncate_at(2).must_equal 4.55
81
+ 4.555.truncate_at(3).must_equal 4.555
82
+ end
83
+
84
+ end
85
+
86
+ describe "#truncate_to" do
87
+
88
+ it "works" do
89
+ 4.555.truncate_to(1).must_equal 4.0
90
+ 4.555.truncate_to(0.1).must_equal 4.5
91
+ 4.555.truncate_to(0.01).must_equal 4.55
92
+ 4.555.truncate_to(0).must_equal 4.555
93
+ end
94
+
95
+ end
96
+
75
97
  end
76
98
 
77
99
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sixarm_ruby_numeric_round
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -35,7 +35,7 @@ cert_chain:
35
35
  L2RORkllUkVIekVSClpEUlFZTXFydTlURU1uYTZIRDl6cGNzdEY3dndUaEdv
36
36
  dmxPUSszWTZwbFE0bk16aXBYY1o5VEhxczY1UElMMHEKZWFid3BDYkFvcG89
37
37
  Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
38
- date: 2012-05-31 00:00:00.000000000 Z
38
+ date: 2012-09-02 00:00:00.000000000 Z
39
39
  dependencies: []
40
40
  description:
41
41
  email: sixarm@sixarm.com
@@ -72,7 +72,7 @@ rubyforge_project:
72
72
  rubygems_version: 1.8.23
73
73
  signing_key:
74
74
  specification_version: 3
75
- summary: SixArm.com » Ruby » Numeric round, floor, ceil methods
75
+ summary: SixArm.com » Ruby » Numeric round, floor, ceil, truncate methods
76
76
  test_files:
77
77
  - test/sixarm_ruby_numeric_round_test.rb
78
78
  has_rdoc: true
metadata.gz.sig CHANGED
@@ -1,2 +1 @@
1
- E�����nU���'S1��t$`�W���Ăonp�coxd�HB��+�+i��s</_�w4�C���-k����W��.g����SWR�d|�ºG��`
2
- M?�Ӽ�N"��a� X)¤ �A��qcO
1
+ cO���2