Class: Float

Inherits:
Object show all
Defined in:
lib/sc-core-ext/float.rb

Instance Method Summary collapse

Instance Method Details

#to_fracObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sc-core-ext/float.rb', line 2

def to_frac
  fraction = case self.abs%1.0
             when 0    then ''
             when 0.25 then '¼'
             when 0.5  then '½'
             when 0.75 then '¾'
             end
  if fraction
    body = case self.floor
           when -1 then '-'
           when  0 then ''
           else self.to_i.to_s
           end
    body + fraction
  else
    if self % 1.0 == 0
      self.to_i.to_s
    else
      self.to_s
    end
  end
end