Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/money/core_extensions.rb
Overview
Allows Writing of ‘100’.to_money for String types Excess characters will be discarded
'100'.to_money => #<Money @cents=10000>
'100.37'.to_money => #<Money @cents=10037>
Instance Method Summary collapse
Instance Method Details
#to_money(currency = nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/money/core_extensions.rb', line 17 def to_money(currency = nil) currency = Money::Helpers.value_to_currency(currency) unless Money.config.legacy_deprecations return Money.new(self, currency) end new_value = BigDecimal(self, exception: false)&.round(currency.minor_units) unless new_value.nil? return Money.new(self, currency) end return Money.new(0, currency) if empty? Money::Parser::Fuzzy.parse(self, currency).tap do |money| old_value = money.value if new_value != old_value = "`\"#{self}\".to_money` will soon behave like `Money.new(\"#{self}\")` and " \ "raise an ArgumentError exception. Use the browser's locale to parse money strings." Money.deprecate() end end end |