Class: Fixnum

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

Instance Method Summary collapse

Instance Method Details

#ordinalizeObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cashflow/patches.rb', line 10

def ordinalize
  if (11..13).include?(self % 100)
    "#{self}th"
  else
    case self % 10
      when 1; "#{self}st"
      when 2; "#{self}nd"
      when 3; "#{self}rd"
      else    "#{self}th"
    end
  end
end

#to_currency(allow_neg = true) ⇒ Object

Assumes integer representing amount in cents.



24
25
26
27
28
29
30
31
# File 'lib/cashflow/patches.rb', line 24

def to_currency(allow_neg=true)
  is_neg = self < 0
  number = self / 100.0
  int, frac = ("%.2f" % number).split('.')
  int.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
  int.slice!(0) if is_neg
  [ '$', int, '.', frac ].join.prepend(allow_neg && is_neg ? '-' : '')
end