vendor/gems/money-2.0.0/lib/money/core_extensions.rb
# Parses the current string and converts it to a Money object.
# Excess characters will be discarded.
#
#   '100'.to_money       # => #<Money @cents=10000>
#   '100.37'.to_money    # => #<Money @cents=10037>
#   '100 USD'.to_money   # => #<Money @cents=10000, @currency="USD">
#   'USD 100'.to_money   # => #<Money @cents=10000, @currency="USD">
#   '$100 USD'.to_money   # => #<Money @cents=10000, @currency="USD">
Parses the current string and converts it to a Money object.
Excess characters will be discarded.


Parses the current string and converts it to a Money object . Excess characters will be discarded .
def to_money
    matches = scan /([A-Z]{2,3})/ 
    currency = matches[0] ? matches[0][0] : Money.default_currency
    matches = scan /(\-?[\d ]+([\.,](\d+))?)/
    cents = if matches[0]
              value = matches[0][0].gsub(/,/, '.')
              value.gsub!(/ +/, '')
              value.to_f * 100
            else
              0
            end
    Money.new(cents, currency)
  end
def to_money matches = scan / ([A-Z]{2,3}) / currency = matches [ 0 ] ? matches [ 0 ] [ 0 ] : Money . default_currency matches = scan / ( \- ?[ \d  ]+([ \. ,]( \d +))?) / cents = if matches [ 0 ] value = matches [ 0 ] [ 0 ] . gsub ( / , / , '.' ) value . gsub! ( /  + / , '' ) value . to_f * 100 else 0 end Money . new ( cents , currency ) end
Parses the current string and converts it to a Money object.
Parses the current string and converts it to a Money object .
# Get the currency. # Get the cents amount