fractional 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e102c37bdea15e535451f4ace3e8427b5cd80ba3
4
- data.tar.gz: aca489da451ff4ded33297324f3691878696dd8a
3
+ metadata.gz: 113941347da803b9e0f57e563c0f63061cd3903b
4
+ data.tar.gz: 4b68768934859d192abc47b3962cc67cecb2f10e
5
5
  SHA512:
6
- metadata.gz: 4dae17fc2ee51bbe1ea07eb0596a759fa5ae34ebd6a9a2418ef9c600c8ff9432a8a5ff354b6f6669edb09f78286e9c8e2432101fbb4712722a66e37eaabf08e5
7
- data.tar.gz: 8eb87fbff148bee9e9c6c39c860c598aa0fd9adc1741688597666c7c77d1b6550b39d87b650b756678131d112ff3e775590a302ef07999347627b74db7524e5c
6
+ metadata.gz: 035b92227c95a2652666c85a52c28b272d32d854388e6475a84b83b85371d38ad823aa0414e660a1d4334d316bb66687d154eafa3ab1d649fabfc79b2cf715f6
7
+ data.tar.gz: 4885f100ddb7365f7b67d65838067044e0daf526f57f8024f762d05cef8dbdbd1f36e71b0a73ced98166d0a6abab399a3daf48836d4bcf27ef69d9b68492628b
data/.rspec CHANGED
@@ -1,3 +1,2 @@
1
1
  --color
2
- --debug
3
2
  --order rand
@@ -11,6 +11,19 @@ Convert floats to fractional strings:
11
11
  Fractional.new(1100.875).to_s #=> "1100 7/8"
12
12
  Fractional.new(1100.875, :to_nearest => "1/2").to_s #=> "1101"
13
13
 
14
+ Convert to human-friendly fractions:
15
+ Fractional.new(-1.331, :to_human).to_s #=> "-1 1/3"
16
+
17
+ Consider the following example:
18
+
19
+ Fractional.new(1.339999).to_s => "67/50"
20
+ Fractional.new(1.339999).to_s(mixed_number: true) => "1 17/50"
21
+
22
+ This is not very human-friendly if we're talking about amounts in cooking recipes, especially when we need to convert from Kg to Pounds etc. Instead use:
23
+
24
+ Fractional.new(1.339999, to_human: true).to_s => "1 1/3"
25
+
26
+
14
27
  Also does a great job of guessing repeating decimal values:
15
28
  Fractional.new(0.33333).to_s #=> "1/3"
16
29
  Fractional.new(3.142857142857).to_s #=> "22/7"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "fractional"
3
- spec.version = "1.0.1"
3
+ spec.version = "1.1.0"
4
4
  spec.authors = ["Chris O'Sullivan"]
5
5
  spec.email = ["thechrisoshow@gmail.com"]
6
6
  spec.description = %q{Fractional is a Ruby library for parsing fractions.}
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.require_paths = ["lib"]
15
15
 
16
16
  spec.add_development_dependency "bundler", "~> 1.3"
17
- spec.add_development_dependency "rake"
18
- spec.add_development_dependency "rspec"
19
- spec.add_development_dependency "debugger"
17
+ spec.add_development_dependency "rake", '~> 0'
18
+ spec.add_development_dependency "rspec", '~> 0'
19
+ spec.add_development_dependency "byebug", '~> 0'
20
20
  end
@@ -14,7 +14,11 @@ class Fractional < Numeric
14
14
  when String
15
15
  @value = Fractional.string_to_fraction( value, options )
16
16
  when Fixnum
17
- @value = Rational(value)
17
+ if @value == @value.to_i
18
+ @value = Rational(value)
19
+ else # It's still Rational if it's a natural number
20
+ @value = Fractional.float_to_fraction( value.to_f, options )
21
+ end
18
22
  when Numeric
19
23
  @value = Fractional.float_to_fraction( value.to_f, options )
20
24
  else
@@ -115,6 +119,10 @@ class Fractional < Numeric
115
119
  return self.round_to_nearest_fraction( value, options[:to_nearest] )
116
120
  end
117
121
 
122
+ if options[:to_human]
123
+ return self.round_to_human_fraction( value )
124
+ end
125
+
118
126
  # first try to convert a repeating decimal unless guesstimate is forbidden
119
127
  unless options[:exact]
120
128
  repeat = float_to_rational_repeat(value)
@@ -217,4 +225,27 @@ class Fractional < Numeric
217
225
  Fractional.new((Fractional.new(value).to_f / to_nearest_float).round * to_nearest_float)
218
226
  end
219
227
 
228
+ def self.numeric_to_mixed_number(amount)
229
+ sign_prefix = ( amount < 0 )? '-' : ''
230
+ amount = amount.abs
231
+ amount_as_integer = amount.to_i
232
+ if (amount_as_integer != amount.to_f) && (amount_as_integer > 0)
233
+ fraction = amount - amount_as_integer
234
+ "#{sign_prefix}#{amount_as_integer} #{fraction}"
235
+ else
236
+ if amount.denominator == 1
237
+ "#{sign_prefix}#{amount_as_integer}"
238
+ else
239
+ sign_prefix + amount.to_s
240
+ end
241
+ end
242
+ end
243
+
244
+ # Display numbers in human-readable manner.
245
+ # Examples: 0.5 -> 1/2, 2.333 -> 2 1/3, 0.666 -> 2/3 etc.
246
+ #
247
+ def self.round_to_human_fraction(value)
248
+ numeric_to_mixed_number value.rationalize(Rational('0.01'))
249
+ end
250
+
220
251
  end
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'byebug'
2
3
 
3
4
  ##################################
4
5
  # CLASS METHODS #
@@ -115,93 +116,93 @@ end
115
116
 
116
117
  describe "Fractional::string_is_fraction?" do
117
118
  it "should recognize a simple fraction" do
118
- Fractional.string_is_fraction?("3/4").should be_true
119
+ Fractional.string_is_fraction?("3/4").should be_truthy
119
120
  end
120
121
 
121
122
  it "should recognize a mixed fraction" do
122
- Fractional.string_is_fraction?("1 11/12").should be_true
123
+ Fractional.string_is_fraction?("1 11/12").should be_truthy
123
124
  end
124
125
 
125
126
  it "should recognize a negative fraction" do
126
- Fractional.string_is_fraction?("-3/4").should be_true
127
+ Fractional.string_is_fraction?("-3/4").should be_truthy
127
128
  end
128
129
 
129
130
  it "should recognize a negative mixed fraction" do
130
- Fractional.string_is_fraction?("-6 2/9").should be_true
131
+ Fractional.string_is_fraction?("-6 2/9").should be_truthy
131
132
  end
132
133
 
133
134
  it "should accept more than one space between the whole number and fractional part" do
134
- Fractional.string_is_fraction?("1 2/3").should be_true
135
- Fractional.string_is_fraction?("3 1/2").should be_true
135
+ Fractional.string_is_fraction?("1 2/3").should be_truthy
136
+ Fractional.string_is_fraction?("3 1/2").should be_truthy
136
137
  end
137
138
 
138
139
  it "should accept fractions with front and rear padding" do
139
- Fractional.string_is_fraction?(" 2/3").should be_true
140
- Fractional.string_is_fraction?("2/3 ").should be_true
141
- Fractional.string_is_fraction?(" 2/3 ").should be_true
142
- Fractional.string_is_fraction?(" 1 2/3").should be_true
143
- Fractional.string_is_fraction?("1 2/3 ").should be_true
144
- Fractional.string_is_fraction?(" 1 2/3 ").should be_true
140
+ Fractional.string_is_fraction?(" 2/3").should be_truthy
141
+ Fractional.string_is_fraction?("2/3 ").should be_truthy
142
+ Fractional.string_is_fraction?(" 2/3 ").should be_truthy
143
+ Fractional.string_is_fraction?(" 1 2/3").should be_truthy
144
+ Fractional.string_is_fraction?("1 2/3 ").should be_truthy
145
+ Fractional.string_is_fraction?(" 1 2/3 ").should be_truthy
145
146
  end
146
147
 
147
148
  it "should not recognize decimals" do
148
- Fractional.string_is_fraction?("2.3").should be_false
149
+ Fractional.string_is_fraction?("2.3").should be_falsey
149
150
  end
150
151
 
151
152
  it "should not recognize two consecutive fractions" do
152
- Fractional.string_is_fraction?("2/3 9/5").should be_false
153
+ Fractional.string_is_fraction?("2/3 9/5").should be_falsey
153
154
  end
154
155
 
155
156
  it "should not recognize a string with a slash" do
156
- Fractional.string_is_fraction?("n/a").should be_false
157
+ Fractional.string_is_fraction?("n/a").should be_falsey
157
158
  end
158
159
 
159
160
  it "should not recognize a fraction mixed with non-decimals" do
160
- Fractional.string_is_fraction?("3a/4").should be_false
161
- Fractional.string_is_fraction?("a2/3").should be_false
162
- Fractional.string_is_fraction?("1 2/3a").should be_false
161
+ Fractional.string_is_fraction?("3a/4").should be_falsey
162
+ Fractional.string_is_fraction?("a2/3").should be_falsey
163
+ Fractional.string_is_fraction?("1 2/3a").should be_falsey
163
164
  end
164
165
 
165
166
  it "should not recognize fractions with improper spacing" do
166
- Fractional.string_is_fraction?("2 /2").should be_false
167
- Fractional.string_is_fraction?("1/ 3").should be_false
168
- Fractional.string_is_fraction?("1 2/ 3").should be_false
169
- Fractional.string_is_fraction?("1 2 /3").should be_false
167
+ Fractional.string_is_fraction?("2 /2").should be_falsey
168
+ Fractional.string_is_fraction?("1/ 3").should be_falsey
169
+ Fractional.string_is_fraction?("1 2/ 3").should be_falsey
170
+ Fractional.string_is_fraction?("1 2 /3").should be_falsey
170
171
  end
171
172
 
172
173
  end
173
174
 
174
175
  describe "Fractional::string_is_mixed_fraction?" do
175
176
  it "should recognize a mixed fraction" do
176
- Fractional.string_is_mixed_fraction?("1 11/12").should be_true
177
+ Fractional.string_is_mixed_fraction?("1 11/12").should be_truthy
177
178
  end
178
179
 
179
180
  it "should recognize a negative mixed fraciton" do
180
- Fractional.string_is_mixed_fraction?("-1 11/12").should be_true
181
+ Fractional.string_is_mixed_fraction?("-1 11/12").should be_truthy
181
182
  end
182
183
 
183
184
  it "should not recognize a single fraction" do
184
- Fractional.string_is_mixed_fraction?("3/4").should be_false
185
+ Fractional.string_is_mixed_fraction?("3/4").should be_falsey
185
186
  end
186
187
 
187
188
  end
188
189
 
189
190
  describe "Fractional::string_is_single_fraction?" do
190
191
  it "should recognize a single fraction" do
191
- Fractional.string_is_single_fraction?("3/4").should be_true
192
+ Fractional.string_is_single_fraction?("3/4").should be_truthy
192
193
  end
193
194
 
194
195
  it "should recognize a negative numerator with a single fraction" do
195
- Fractional.string_is_single_fraction?("-3/4").should be_true
196
+ Fractional.string_is_single_fraction?("-3/4").should be_truthy
196
197
  end
197
198
 
198
199
  it "should not recognize a mixed fraction" do
199
- Fractional.string_is_single_fraction?("1 11/12").should be_false
200
+ Fractional.string_is_single_fraction?("1 11/12").should be_falsey
200
201
  end
201
202
 
202
203
  it "should allow for negative denominators in single fractions" do
203
- Fractional.string_is_single_fraction?("1/-64").should be_true
204
- Fractional.string_is_single_fraction?("-1/-64").should be_true
204
+ Fractional.string_is_single_fraction?("1/-64").should be_truthy
205
+ Fractional.string_is_single_fraction?("-1/-64").should be_truthy
205
206
  end
206
207
 
207
208
  end
@@ -313,15 +314,15 @@ end
313
314
 
314
315
  describe "Fractional#==" do
315
316
  it "should be equal to same Fractions" do
316
- (Fractional.new(Rational(3,1)) == Fractional.new(Rational(3,1))).should be_true
317
+ (Fractional.new(Rational(3,1)) == Fractional.new(Rational(3,1))).should be_truthy
317
318
  end
318
319
 
319
320
  it "should not be equal to other Fractions" do
320
- (Fractional.new(Rational(3,2)) == Fractional.new(Rational(3,1))).should be_false
321
+ (Fractional.new(Rational(3,2)) == Fractional.new(Rational(3,1))).should be_falsey
321
322
  end
322
323
 
323
324
  it "should be equal regardless of type" do
324
- (Fractional.new(Rational(3,4)) == Fractional.new(0.75)).should be_true
325
+ (Fractional.new(Rational(3,4)) == Fractional.new(0.75)).should be_truthy
325
326
  end
326
327
  end
327
328
 
@@ -355,7 +356,7 @@ end
355
356
 
356
357
  describe "Frational", "comparsion" do
357
358
  it "should use the numerics included comparsion module" do
358
- (Fractional.new(Rational(-3,4)) < Fractional.new(Rational(1,2))).should be_true
359
+ (Fractional.new(Rational(-3,4)) < Fractional.new(Rational(1,2))).should be_truthy
359
360
  end
360
361
  end
361
362
 
@@ -367,7 +368,6 @@ describe "Fractional#to_s" do
367
368
  it "should return nice representations of single fractions" do
368
369
  Fractional.new(0.75).to_s.should == "3/4"
369
370
  Fractional.new(-0.3333).to_s.should == "-1/3"
370
-
371
371
  end
372
372
 
373
373
  it "should return nice representations of mixed fractions" do
@@ -434,6 +434,23 @@ describe "Fractional", "round" do
434
434
  it "should round if passed a float" do
435
435
  Fractional.round_to_nearest_fraction(1100.875, 0.5).should == Fractional.new(1101)
436
436
  end
437
+
438
+ it "should round if passed a float" do
439
+ Fractional.round_to_human_fraction(0.9001).to_s.should == "9/10"
440
+ Fractional.round_to_human_fraction(-0.8012).to_s.should == "-4/5"
441
+ Fractional.round_to_human_fraction(0.337).to_s.should == "1/3"
442
+ Fractional.round_to_human_fraction(-1.331).to_s.should == "-1 1/3"
443
+ Fractional.round_to_human_fraction(5.691).to_s.should == "5 7/10"
444
+ Fractional.round_to_human_fraction(-3.668).to_s.should == "-3 2/3"
445
+ Fractional.round_to_human_fraction(2.0).to_s.should == "2"
446
+ Fractional.round_to_human_fraction(11).to_s.should == "11"
447
+ end
448
+
449
+ it "should round if passed 'to_human' that rounds to nearest whole number" do
450
+ Fractional.new(2.0, to_human: true).to_s.should == "2"
451
+ Fractional.new(-2.0, to_human: true).to_s.should == "-2"
452
+ end
453
+
437
454
  end
438
455
 
439
456
  ##################################
@@ -473,14 +490,14 @@ describe "deprecated methods" do
473
490
  end
474
491
 
475
492
  it "Fraction.fraction?" do
476
- Fractional.fraction?("3/4").should be_true
493
+ Fractional.fraction?("3/4").should be_truthy
477
494
  end
478
495
 
479
496
  it "Fraction.mixed_fraction?" do
480
- Fractional.mixed_fraction?("1 11/12").should be_true
497
+ Fractional.mixed_fraction?("1 11/12").should be_truthy
481
498
  end
482
499
 
483
500
  it "Fraction.single_fraction?" do
484
- Fractional.single_fraction?("-3/4").should be_true
501
+ Fractional.single_fraction?("-3/4").should be_truthy
485
502
  end
486
- end
503
+ end
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fractional
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris O'Sullivan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-13 00:00:00.000000000 Z
11
+ date: 2015-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: debugger
56
+ name: byebug
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: Fractional is a Ruby library for parsing fractions.
@@ -73,8 +73,8 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
- - .gitignore
77
- - .rspec
76
+ - ".gitignore"
77
+ - ".rspec"
78
78
  - Gemfile
79
79
  - LICENSE
80
80
  - README.rdoc
@@ -95,17 +95,17 @@ require_paths:
95
95
  - lib
96
96
  required_ruby_version: !ruby/object:Gem::Requirement
97
97
  requirements:
98
- - - '>='
98
+ - - ">="
99
99
  - !ruby/object:Gem::Version
100
100
  version: '0'
101
101
  required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  requirements:
103
- - - '>='
103
+ - - ">="
104
104
  - !ruby/object:Gem::Version
105
105
  version: '0'
106
106
  requirements: []
107
107
  rubyforge_project:
108
- rubygems_version: 2.0.3
108
+ rubygems_version: 2.4.8
109
109
  signing_key:
110
110
  specification_version: 4
111
111
  summary: 'You can use fractional to convert decimal numbers to string representations