Class: Float

Inherits:
Object
  • Object
show all
Defined in:
lib/to_html_fraction.rb

Instance Method Summary collapse

Instance Method Details

#to_html_fractionObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/to_html_fraction.rb', line 2

def to_html_fraction
  body = case self.floor
    when -1 then '-'
    when  0 then ''
    else self.to_i.to_s
  end
  
  fraction = case ((self.abs % 1.0) * 16).round.to_f / 16 #round to 1/16th precision. 
    when 0.0625 then ' 1/16'
    when 0.125  then '⅛'  # One eighth
    when 0.1875 then ' 3/16'
    when 0.25   then '¼'  # One quarter
    when 0.3125 then ' 5/16'
    when 0.375  then '⅜'  # Three eighths
    when 0.4375 then ' 7/16'
    when 0.5    then '½'  # One half
    when 0.5625 then ' 9/16'
    when 0.625  then '⅝'  # Five eighths
    when 0.6875 then ' 11/16'
    when 0.75   then '¾'  # Three quarters
    when 0.8125 then ' 13/16'
    when 0.875  then '⅞'  # Seven eighths
    when 0.9375 then ' 15/16'
    else ''
  end
  
  body + fraction
end