to_html_fraction 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +5 -1
- data/VERSION +1 -1
- data/lib/to_html_fraction.rb +22 -31
- data/spec/to_html_fraction_spec.rb +19 -10
- data/to_html_fraction.gemspec +1 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
= to_html_fraction
|
2
2
|
|
3
|
-
|
3
|
+
Originally take from here http://snippets.dzone.com/posts/show/2323
|
4
|
+
|
5
|
+
Added some tests and turned it into a gem.
|
6
|
+
|
7
|
+
Also updated it so that it will round decimals to nearest 1/16th, because I never have need for fractions like 1/3, 3/5.
|
4
8
|
|
5
9
|
== Note on Patches/Pull Requests
|
6
10
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/to_html_fraction.rb
CHANGED
@@ -1,39 +1,30 @@
|
|
1
1
|
class Float
|
2
2
|
def to_html_fraction
|
3
|
-
|
4
|
-
when 0.5 then '½' # One half
|
5
|
-
|
6
|
-
when 0.25 then '¼' # One quarter
|
7
|
-
when 0.75 then '¾' # Three quarters
|
8
|
-
|
9
|
-
when 0.33...0.333333333333333333 then '⅓' # One third
|
10
|
-
when 0.66...0.666666666666666666 then '⅔' # Two thirds
|
11
|
-
|
12
|
-
when 0.2 then '⅕' # One fifth
|
13
|
-
when 0.4 then '⅖' # Two fifths
|
14
|
-
when 0.6 then '⅗' # Three fifths
|
15
|
-
when 0.8 then '⅘' # Four fifths
|
16
|
-
|
17
|
-
when 0.16...0.166666666666666666 then '⅙' # One sixth
|
18
|
-
when 0.83...0.833333333333333333 then '⅚' # Five sixths
|
19
|
-
|
20
|
-
when 0.125 then '⅛' # One eighth
|
21
|
-
when 0.375 then '⅜' # Three eighths
|
22
|
-
when 0.625 then '⅝' # Five eighths
|
23
|
-
when 0.875 then '⅞' # Seven eighths
|
24
|
-
|
25
|
-
when 0.18...0.1875 then ' 3/16'
|
26
|
-
when 0.0 then ''
|
27
|
-
end
|
28
|
-
if fraction
|
29
|
-
body = case self.floor
|
3
|
+
body = case self.floor
|
30
4
|
when -1 then '-'
|
31
5
|
when 0 then ''
|
32
6
|
else self.to_i.to_s
|
33
|
-
end
|
34
|
-
body + fraction
|
35
|
-
else
|
36
|
-
self.to_s
|
37
7
|
end
|
8
|
+
|
9
|
+
fraction = case ((self.abs % 1.0) * 16).round.to_f / 16 #round to 1/16th precision.
|
10
|
+
when 0.0625 then ' 1/16'
|
11
|
+
when 0.125 then '⅛' # One eighth
|
12
|
+
when 0.1875 then ' 3/16'
|
13
|
+
when 0.25 then '¼' # One quarter
|
14
|
+
when 0.3125 then ' 5/16'
|
15
|
+
when 0.375 then '⅜' # Three eighths
|
16
|
+
when 0.4375 then ' 7/16'
|
17
|
+
when 0.5 then '½' # One half
|
18
|
+
when 0.5625 then ' 9/16'
|
19
|
+
when 0.625 then '⅝' # Five eighths
|
20
|
+
when 0.6875 then ' 11/16'
|
21
|
+
when 0.75 then '¾' # Three quarters
|
22
|
+
when 0.8125 then ' 13/16'
|
23
|
+
when 0.875 then '⅞' # Seven eighths
|
24
|
+
when 0.9375 then ' 15/16'
|
25
|
+
else ''
|
26
|
+
end
|
27
|
+
|
28
|
+
body + fraction
|
38
29
|
end
|
39
30
|
end
|
@@ -1,22 +1,31 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "ToHtmlFraction" do
|
4
|
-
it "should convert a
|
4
|
+
it "should convert a float to an html fraction" do
|
5
5
|
# pending
|
6
|
+
0.0625.to_html_fraction.should == ' 1/16'
|
6
7
|
0.125.to_html_fraction.should == '⅛'
|
7
|
-
0.
|
8
|
-
0.2.to_html_fraction.should == '⅕'
|
8
|
+
0.1875.to_html_fraction.should == ' 3/16'
|
9
9
|
0.25.to_html_fraction.should == '¼'
|
10
|
-
0.
|
10
|
+
0.3125.to_html_fraction.should == ' 5/16'
|
11
11
|
0.375.to_html_fraction.should == '⅜'
|
12
|
-
0.
|
12
|
+
0.4375.to_html_fraction.should == ' 7/16'
|
13
13
|
0.5.to_html_fraction.should == '½'
|
14
|
-
0.
|
14
|
+
0.5625.to_html_fraction.should == ' 9/16'
|
15
15
|
0.625.to_html_fraction.should == '⅝'
|
16
|
-
0.
|
16
|
+
0.6875.to_html_fraction.should == ' 11/16'
|
17
17
|
0.75.to_html_fraction.should == '¾'
|
18
|
-
0.
|
19
|
-
0.833.to_html_fraction.should == '⅚'
|
18
|
+
0.8125.to_html_fraction.should == ' 13/16'
|
20
19
|
0.875.to_html_fraction.should == '⅞'
|
20
|
+
0.9375.to_html_fraction.should == ' 15/16'
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should round uncommon decimals to the nearest 1/16th" do
|
25
|
+
0.166.to_html_fraction.should == ' 3/16'
|
26
|
+
0.3.to_html_fraction.should == ' 5/16'
|
27
|
+
0.503.to_html_fraction.should == '½'
|
28
|
+
0.68.to_html_fraction.should == ' 11/16'
|
29
|
+
0.975.to_html_fraction.should == ''
|
21
30
|
end
|
22
|
-
end
|
31
|
+
end
|
data/to_html_fraction.gemspec
CHANGED