2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/money_extension.rb', line 2
def monetize(options={})
options = {:currency_symbol => "$", :delimiter => ",", :decimal => ".",
:currency_before => true, :no_decimal => false}.merge(options)
if self.class == Bignum
parts = self.to_s.gsub(/[^0-9^.]/, '').split('.')
int = parts[0] or '0'
frac = parts[1].nil? ? '00' : parts[1]
else
int, frac = ("%.2f" % self.to_s.gsub(/[^0-9^.]/, '')).split('.')
end
int.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
frac_part = options[:no_decimal] ? "" : options[:decimal] + frac
if options[:currency_before]
options[:currency_symbol] + int + frac_part
else
int + frac_part + options[:currency_symbol]
end
end
|