Module: SimpleModel::ToCurrencyS

Defined in:
lib/simple_model/extend_core.rb

Instance Method Summary collapse

Instance Method Details

#to_currency_s(symbol = '$', rnd = 2) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/simple_model/extend_core.rb', line 3

def to_currency_s(symbol='$',rnd=2)
  cs = (rnd ? self.round(rnd) : self).abs.to_s
  while cs.index('.') != (cs.length-3)
    cs << '0'
  end
  comma = 6
  while cs.length > (comma)
    cs.insert((cs.length - comma), ",")
    comma += 4
  end
  cs.insert(0,symbol) if symbol
  if self < 0
    cs.insert(0, "-")
  end
  cs
end