Sha256: 4f0bfe3066c5820f526d6660c77f07321b12b760db47ee9109f3ca97b8c65f08
Contents?: true
Size: 1.52 KB
Versions: 3
Compression:
Stored size: 1.52 KB
Contents
class Numeric # Converts this numeric to a Money object in the default currency. It # multiplies the numeric value by 100 and treats that as cents. # # 100.to_money => #<Money @cents=10000> # 100.37.to_money => #<Money @cents=10037> # 1000.to_money(:millicents) => #<Money @cents=1> def to_money(units=:cents) if units == :millicents Money.new(self, :units => :millicents) else Money.new(self * 100) end end end class String # 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"> # '1000'.to_money(:millicents) # => #<Money @cents=1, @currency="USD"> def to_money(units=:cents) # Get the currency. matches = scan /([A-Z]{2,3})/ currency = matches[0] ? matches[0][0] : Money.default_currency # Get the cents amount sans_spaces = gsub(/\s+/, '') matches = sans_spaces.scan /(\-?\d+(?:[\.,]\d+)?)/ money = if matches[0] value = matches[0][0].gsub(/,/, '.') value.to_f else 0 end if units == :millicents Money.new(money, currency, :units => :millicents) else Money.new(money * 100, currency) end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
sevenwire-money-2.1.0 | lib/money/core_extensions.rb |
sevenwire-money-2.1.1 | lib/money/core_extensions.rb |
sevenwire-money-2.2.0 | lib/money/core_extensions.rb |