Class: Numeric

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

Instance Method Summary collapse

Instance Method Details

#to_money(cents = false) ⇒ Object

Converts this numeric to a Money object in the default currency. It multiplies the numeric value by 100 and treats that as cents if receive false.

100.to_money => #<Money @cents=100>
100.37.to_money => #<Money @cents=10037>
100.to_money(false) => #<Money @cents=10000>


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/money/core_extensions.rb', line 8

def to_money(cents = false)
  if cents
    if self.is_a? Integer
      Money.new(self)
    else
      Money.new(self.to_s.gsub(/\./,'').to_i)
    end
  else
    Money.new((self * 100).round)
  end
end