sixarm_ruby_numeric_round 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/.gemtest +0 -0
- data/README.md +105 -0
- data/Rakefile +8 -0
- data/VERSION +1 -0
- data/lib/sixarm_ruby_numeric_round.rb +141 -0
- data/test/sixarm_ruby_numeric_round_test.rb +82 -0
- metadata +78 -0
- metadata.gz.sig +2 -0
data.tar.gz.sig
ADDED
Binary file
|
data/.gemtest
ADDED
File without changes
|
data/README.md
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# SixArm.com » Ruby » <br> Numeric rounding methods
|
2
|
+
|
3
|
+
* Doc: <http://sixarm.com/sixarm_ruby_numeric_round/doc>
|
4
|
+
* Gem: <http://rubygems.org/gems/sixarm_ruby_numeric_round>
|
5
|
+
* Repo: <http://github.com/sixarm/sixarm_ruby_numeric_round>
|
6
|
+
* Email: Joel Parker Henderson, <joel@sixarm.com>
|
7
|
+
|
8
|
+
|
9
|
+
## Introduction
|
10
|
+
|
11
|
+
Simple numeric rounding methods like #round, #floor, #ceil with various precisions.
|
12
|
+
|
13
|
+
For docs go to <http://sixarm.com/sixarm_ruby_numeric_round/doc>
|
14
|
+
|
15
|
+
Want to help? We're happy to get pull requests.
|
16
|
+
|
17
|
+
|
18
|
+
## Install quickstart
|
19
|
+
|
20
|
+
Install:
|
21
|
+
|
22
|
+
gem install sixarm_ruby_numeric_round
|
23
|
+
|
24
|
+
Bundler:
|
25
|
+
|
26
|
+
gem "sixarm_ruby_numeric_round", "~>1.0.0"
|
27
|
+
|
28
|
+
Require:
|
29
|
+
|
30
|
+
require "sixarm_ruby_numeric_round"
|
31
|
+
|
32
|
+
|
33
|
+
## Install with security (optional)
|
34
|
+
|
35
|
+
To enable high security for all our gems:
|
36
|
+
|
37
|
+
wget http://sixarm.com/sixarm.pem
|
38
|
+
gem cert --add sixarm.pem
|
39
|
+
gem sources --add http://sixarm.com
|
40
|
+
|
41
|
+
To install with high security:
|
42
|
+
|
43
|
+
gem install sixarm_ruby_numeric_round --test --trust-policy HighSecurity
|
44
|
+
|
45
|
+
|
46
|
+
## Examples
|
47
|
+
|
48
|
+
Round at a given decimal point:
|
49
|
+
|
50
|
+
4.555.ceil_at(1) #=> 4.6
|
51
|
+
4.555.floor_at(1) #=> 4.5
|
52
|
+
4.555.round_at(1) #=> 4.6
|
53
|
+
|
54
|
+
Round to a given precision:
|
55
|
+
|
56
|
+
4.555.ceil_to(0.1) #=> 4.6
|
57
|
+
4.555.floor_to(0.1) #=> 4.5
|
58
|
+
4.555.round_to(0.1) #=> 4.6
|
59
|
+
|
60
|
+
Conceptually, the methods for round, floor, and ceil will typically be for floating point numbers.
|
61
|
+
However, these methods can actually be for pretty much any Numeric object.
|
62
|
+
For example, one could round an Integer to the nearest kilo.
|
63
|
+
|
64
|
+
|
65
|
+
## Credit
|
66
|
+
|
67
|
+
These methods are based on the Facets library for Ruby and the developer named Trans.
|
68
|
+
|
69
|
+
The #round_at and #round_to methods are exact copies and fully compatible with Facets. The rest of the methods are very similar code and fully co-existant with Facets.
|
70
|
+
|
71
|
+
We're asking the Facets team to consider including the rest of the methods.
|
72
|
+
|
73
|
+
|
74
|
+
## Changes
|
75
|
+
|
76
|
+
* 2012-05-30 1.0.0 Create baesd on Facets methods by Trans.
|
77
|
+
|
78
|
+
|
79
|
+
## License
|
80
|
+
|
81
|
+
You may choose any of these open source licenses:
|
82
|
+
|
83
|
+
* Apache License
|
84
|
+
* BSD License
|
85
|
+
* CreativeCommons License, Non-commercial Share Alike
|
86
|
+
* GNU General Public License Version 2 (GPL 2)
|
87
|
+
* GNU Lesser General Public License (LGPL)
|
88
|
+
* MIT License
|
89
|
+
* Perl Artistic License
|
90
|
+
* Ruby License
|
91
|
+
|
92
|
+
The software is provided "as is", without warranty of any kind,
|
93
|
+
express or implied, including but not limited to the warranties of
|
94
|
+
merchantability, fitness for a particular purpose and noninfringement.
|
95
|
+
|
96
|
+
In no event shall the authors or copyright holders be liable for any
|
97
|
+
claim, damages or other liability, whether in an action of contract,
|
98
|
+
tort or otherwise, arising from, out of or in connection with the
|
99
|
+
software or the use or other dealings in the software.
|
100
|
+
|
101
|
+
This license is for the included software that is created by SixArm;
|
102
|
+
some of the included software may have its own licenses, copyrights,
|
103
|
+
authors, etc. and these do take precedence over the SixArm license.
|
104
|
+
|
105
|
+
Copyright (c) 2005-2012 Joel Parker Henderson
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
=begin rdoc
|
3
|
+
Please see README
|
4
|
+
=end
|
5
|
+
|
6
|
+
class Numeric
|
7
|
+
|
8
|
+
# Conceptually, ceil is expected to apply to floating point numbers.
|
9
|
+
# However it can actually be applied to pretty much any Numeric object.
|
10
|
+
# For example, one could ceil an Integer to the nearest kilo.
|
11
|
+
#
|
12
|
+
# See Float#ceil_at.
|
13
|
+
|
14
|
+
def ceil_at(*args)
|
15
|
+
to_f.ceil_at(*args)
|
16
|
+
end
|
17
|
+
|
18
|
+
# See Float#ceil_to.
|
19
|
+
|
20
|
+
def ceil_to(*args)
|
21
|
+
to_f.ceil_to(*args)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Conceptually, floor is expected to apply to floating point numbers.
|
25
|
+
# However it can actually be applied to pretty much any Numeric object.
|
26
|
+
# For example, one could floor an Integer to the nearest kilo.
|
27
|
+
#
|
28
|
+
# See Float#floor_at.
|
29
|
+
|
30
|
+
def floor_at(*args)
|
31
|
+
to_f.floor_at(*args)
|
32
|
+
end
|
33
|
+
|
34
|
+
# See Float#floor_to.
|
35
|
+
|
36
|
+
def floor_to(*args)
|
37
|
+
to_f.floor_to(*args)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Conceptually, round is expected to apply to floating point numbers.
|
41
|
+
# However it can actually be applied to pretty much any Numeric object.
|
42
|
+
# For example, one could round an Integer to the nearest kilo.
|
43
|
+
#
|
44
|
+
# See Float#round_at.
|
45
|
+
|
46
|
+
def round_at(*args)
|
47
|
+
to_f.round_at(*args)
|
48
|
+
end
|
49
|
+
|
50
|
+
# See Float#round_to.
|
51
|
+
|
52
|
+
def round_to(*args)
|
53
|
+
to_f.round_to(*args)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
class Float
|
59
|
+
|
60
|
+
# Ceil to the given decimal position.
|
61
|
+
#
|
62
|
+
# 4.555.ceil_at(0) #=> 5.0
|
63
|
+
# 4.555.ceil_at(1) #=> 4.6
|
64
|
+
# 4.555.ceil_at(2) #=> 4.56
|
65
|
+
# 4.555.ceil_at(3) #=> 4.555
|
66
|
+
#
|
67
|
+
# CREDIT: Trans & Joel Parker Henderson
|
68
|
+
|
69
|
+
def ceil_at( d ) #d=0
|
70
|
+
(self * (10.0 ** d)).ceil.to_f / (10.0 ** d)
|
71
|
+
end
|
72
|
+
|
73
|
+
# Ceils to the nearest _n_th degree.
|
74
|
+
#
|
75
|
+
# 4.555.ceil_to(1) #=> 5.0
|
76
|
+
# 4.555.ceil_to(0.1) #=> 4.6
|
77
|
+
# 4.555.ceil_to(0.01) #=> 4.56
|
78
|
+
# 4.555.ceil_to(0) #=> 4.555
|
79
|
+
#
|
80
|
+
# CREDIT: Trans & Joel Parker Henderson
|
81
|
+
|
82
|
+
def ceil_to( n ) #n=1
|
83
|
+
return self if n == 0
|
84
|
+
(self * (1.0 / n)).ceil.to_f / (1.0 / n)
|
85
|
+
end
|
86
|
+
|
87
|
+
# Floor to the given decimal position.
|
88
|
+
#
|
89
|
+
# 4.555.floor_at(0) #=> 4.0
|
90
|
+
# 4.555.floor_at(1) #=> 4.5
|
91
|
+
# 4.555.floor_at(2) #=> 4.55
|
92
|
+
# 4.555.floor_at(3) #=> 4.555
|
93
|
+
#
|
94
|
+
# CREDIT: Trans & Joel Parker Henderson
|
95
|
+
|
96
|
+
def floor_at( d ) #d=0
|
97
|
+
(self * (10.0 ** d)).floor.to_f / (10.0 ** d)
|
98
|
+
end
|
99
|
+
|
100
|
+
# Floors to the nearest _n_th degree.
|
101
|
+
#
|
102
|
+
# 4.555.floor_to(1) #=> 4.0
|
103
|
+
# 4.555.floor_to(0.1) #=> 4.5
|
104
|
+
# 4.555.floor_to(0.01) #=> 4.55
|
105
|
+
# 4.555.floor_to(0) #=> 4.555
|
106
|
+
#
|
107
|
+
# CREDIT: Trans & Joel Parker Henderson
|
108
|
+
|
109
|
+
def floor_to( n ) #n=1
|
110
|
+
return self if n == 0
|
111
|
+
(self * (1.0 / n)).floor.to_f / (1.0 / n)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Round to the given decimal position.
|
115
|
+
#
|
116
|
+
# 4.555.round_at(0) #=> 5.0
|
117
|
+
# 4.555.round_at(1) #=> 4.6
|
118
|
+
# 4.555.round_at(2) #=> 4.56
|
119
|
+
# 4.555.round_at(3) #=> 4.555
|
120
|
+
#
|
121
|
+
# CREDIT: Trans & Joel Parker Henderson
|
122
|
+
|
123
|
+
def round_at( d ) #d=0
|
124
|
+
(self * (10.0 ** d)).round.to_f / (10.0 ** d)
|
125
|
+
end
|
126
|
+
|
127
|
+
# Rounds to the nearest _n_th degree.
|
128
|
+
#
|
129
|
+
# 4.555.round_to(1) #=> 5
|
130
|
+
# 4.555.round_to(0.1) #=> 4.6
|
131
|
+
# 4.555.round_to(0.01) #=> 4.56
|
132
|
+
# 4.555.round_to(0) #=> 4.555
|
133
|
+
#
|
134
|
+
# CREDIT: Trans & Joel Parker Henderson
|
135
|
+
|
136
|
+
def round_to( n ) #n=1
|
137
|
+
return self if n == 0
|
138
|
+
(self * (1.0 / n)).round.to_f / (1.0 / n)
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'simplecov'
|
4
|
+
SimpleCov.start
|
5
|
+
require 'sixarm_ruby_numeric_round'
|
6
|
+
|
7
|
+
describe Float do
|
8
|
+
|
9
|
+
describe "#ceil_at" do
|
10
|
+
|
11
|
+
it "works" do
|
12
|
+
4.555.ceil_at(0).must_equal 5.0
|
13
|
+
4.555.ceil_at(1).must_equal 4.6
|
14
|
+
4.555.ceil_at(2).must_equal 4.56
|
15
|
+
4.555.ceil_at(3).must_equal 4.555
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#ceil_to" do
|
21
|
+
|
22
|
+
it "works" do
|
23
|
+
4.555.ceil_to(1).must_equal 5.0
|
24
|
+
4.555.ceil_to(0.1).must_equal 4.6
|
25
|
+
4.555.ceil_to(0.01).must_equal 4.56
|
26
|
+
4.555.ceil_to(0).must_equal 4.555
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#floor_at" do
|
32
|
+
|
33
|
+
it "works" do
|
34
|
+
4.555.floor_at(0).must_equal 4.0
|
35
|
+
4.555.floor_at(1).must_equal 4.5
|
36
|
+
4.555.floor_at(2).must_equal 4.55
|
37
|
+
4.555.floor_at(3).must_equal 4.555
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#floor_to" do
|
43
|
+
|
44
|
+
it "works" do
|
45
|
+
4.555.floor_to(1).must_equal 4.0
|
46
|
+
4.555.floor_to(0.1).must_equal 4.5
|
47
|
+
4.555.floor_to(0.01).must_equal 4.55
|
48
|
+
4.555.floor_to(0).must_equal 4.555
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#round_at" do
|
54
|
+
|
55
|
+
it "works" do
|
56
|
+
4.555.round_at(0).must_equal 5.0
|
57
|
+
4.555.round_at(1).must_equal 4.6
|
58
|
+
4.555.round_at(2).must_equal 4.56
|
59
|
+
4.555.round_at(3).must_equal 4.555
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "#round_to" do
|
65
|
+
|
66
|
+
it "works" do
|
67
|
+
4.555.round_to(1).must_equal 5
|
68
|
+
4.555.round_to(0.1).must_equal 4.6
|
69
|
+
4.555.round_to(0.01).must_equal 4.56
|
70
|
+
4.555.round_to(0).must_equal 4.555
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sixarm_ruby_numeric_round
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- SixArm
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain:
|
12
|
+
- !binary |-
|
13
|
+
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURCRENDQW0yZ0F3SUJB
|
14
|
+
Z0lKQUtQd0VFVFU1YkhvTUEwR0NTcUdTSWIzRFFFQkJRVUFNR0F4Q3pBSkJn
|
15
|
+
TlYKQkFZVEFsVlRNUk13RVFZRFZRUUlFd3BEWVd4cFptOXlibWxoTVJZd0ZB
|
16
|
+
WURWUVFIRXcxVFlXNGdSbkpoYm1OcApjMk52TVE4d0RRWURWUVFLRXdaVGFY
|
17
|
+
aEJjbTB4RXpBUkJnTlZCQU1UQ25OcGVHRnliUzVqYjIwd0hoY05NVEF4Ck1q
|
18
|
+
RXpNak15TnpFeldoY05NVE13T1RBNE1qTXlOekV6V2pCZ01Rc3dDUVlEVlFR
|
19
|
+
R0V3SlZVekVUTUJFR0ExVUUKQ0JNS1EyRnNhV1p2Y201cFlURVdNQlFHQTFV
|
20
|
+
RUJ4TU5VMkZ1SUVaeVlXNWphWE5qYnpFUE1BMEdBMVVFQ2hNRwpVMmw0UVhK
|
21
|
+
dE1STXdFUVlEVlFRREV3cHphWGhoY20wdVkyOXRNSUdmTUEwR0N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
Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
|
38
|
+
date: 2012-05-31 00:00:00.000000000 Z
|
39
|
+
dependencies: []
|
40
|
+
description:
|
41
|
+
email: sixarm@sixarm.com
|
42
|
+
executables: []
|
43
|
+
extensions: []
|
44
|
+
extra_rdoc_files: []
|
45
|
+
files:
|
46
|
+
- .gemtest
|
47
|
+
- Rakefile
|
48
|
+
- README.md
|
49
|
+
- VERSION
|
50
|
+
- lib/sixarm_ruby_numeric_round.rb
|
51
|
+
- test/sixarm_ruby_numeric_round_test.rb
|
52
|
+
homepage: http://sixarm.com/
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.23
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: SixArm.com » Ruby » Numeric round, floor, ceil methods
|
76
|
+
test_files:
|
77
|
+
- test/sixarm_ruby_numeric_round_test.rb
|
78
|
+
has_rdoc: true
|
metadata.gz.sig
ADDED