Module | Money::Parsing::ClassMethods |
In: |
lib/money/money/parsing.rb
|
Takes a number string and attempts to massage out the number.
@param [String] input The string containing a potential number.
@return [Integer]
Converts a BigDecimal into a Money object treating the value
as dollars and converting them to the corresponding cents value, according to +currency+ subunit property, before instantiating the Money object. @param [BigDecimal] value The money amount, in dollars. @param [Currency, String, Symbol] currency The currency format. @return [Money] @example Money.from_bigdecimal(BigDecimal.new("100") #=> #<Money @cents=10000 @currency="USD"> Money.from_bigdecimal(BigDecimal.new("100", "USD") #=> #<Money @cents=10000 @currency="USD"> Money.from_bigdecimal(BigDecimal.new("100", "EUR") #=> #<Money @cents=10000 @currency="EUR"> Money.from_bigdecimal(BigDecimal.new("100", "BHD") #=> #<Money @cents=100 @currency="BHD"> @see BigDecimal#to_money @see Money.from_numeric
Converts a Fixnum into a Money object treating the value
as dollars and converting them to the corresponding cents value, according to +currency+ subunit property, before instantiating the Money object. @param [Fixnum] value The money amount, in dollars. @param [Currency, String, Symbol] currency The currency format. @return [Money] @example Money.from_fixnum(100) #=> #<Money @cents=10000 @currency="USD"> Money.from_fixnum(100, "USD") #=> #<Money @cents=10000 @currency="USD"> Money.from_fixnum(100, "EUR") #=> #<Money @cents=10000 @currency="EUR"> Money.from_fixnum(100, "BHD") #=> #<Money @cents=100 @currency="BHD"> @see Fixnum#to_money @see Money.from_numeric
Converts a Float into a Money object treating the value
as dollars and converting them to the corresponding cents value, according to +currency+ subunit property, before instantiating the Money object. Behind the scenes, this method relies on Money.from_bigdecimal to avoid problems with floating point precision. @param [Float] value The money amount, in dollars. @param [Currency, String, Symbol] currency The currency format. @return [Money] @example Money.from_float(100.0) #=> #<Money @cents=10000 @currency="USD"> Money.from_float(100.0, "USD") #=> #<Money @cents=10000 @currency="USD"> Money.from_float(100.0, "EUR") #=> #<Money @cents=10000 @currency="EUR"> Money.from_float(100.0, "BHD") #=> #<Money @cents=100 @currency="BHD"> @see Float#to_money @see Money.from_numeric
Converts a Numeric value into a Money object treating the value
as dollars and converting them to the corresponding cents value, according to +currency+ subunit property, before instantiating the Money object. This method relies on various +Money.from_*+ methods and tries to forwards the call to the most appropriate method in order to reduce computation effort. For instance, if +value+ is an Integer, this method calls {Money.from_fixnum} instead of using the default {Money.from_bigdecimal} which adds the overload to converts the value into a slower BigDecimal instance. @param [Numeric] value The money amount, in dollars. @param [Currency, String, Symbol] currency The currency format. @return [Money] @raise +ArgumentError+ Unless +value+ is a supported type. @example Money.from_numeric(100) #=> #<Money @cents=10000 @currency="USD"> Money.from_numeric(100.00) #=> #<Money @cents=10000 @currency="USD"> Money.from_numeric("100") #=> ArgumentError @see Numeric#to_money @see Money.from_fixnum @see Money.from_float @see Money.from_bigdecimal
Converts a String into a Money object treating the value
as dollars and converting them to the corresponding cents value, according to +currency+ subunit property, before instantiating the Money object. Behind the scenes, this method relies on {Money.from_bigdecimal} to avoid problems with string-to-numeric conversion. @param [String, #to_s] value The money amount, in dollars. @param [Currency, String, Symbol] currency The currency to set the resulting +Money+ object to. @return [Money] @example Money.from_string("100") #=> #<Money @cents=10000 @currency="USD"> Money.from_string("100", "USD") #=> #<Money @cents=10000 @currency="USD"> Money.from_string("100", "EUR") #=> #<Money @cents=10000 @currency="EUR"> Money.from_string("100", "BHD") #=> #<Money @cents=100 @currency="BHD"> @see String#to_money @see Money.parse
Parses the current string and converts it to a Money object. Excess characters will be discarded.
@param [String, to_s] input The input to parse. @param [Currency, String, Symbol] currency The currency format.
The currency to set the resulting +Money+ object to.
@return [Money]
@raise [ArgumentError] If any currency is supplied and
given value doesn't match the one extracted from the +input+ string.
@example
'100'.to_money #=> #<Money @cents=10000> '100.37'.to_money #=> #<Money @cents=10037> '100 USD'.to_money #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>> 'USD 100'.to_money #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>> '$100 USD'.to_money #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>> 'hello 2000 world'.to_money #=> #<Money @cents=200000 @currency=#<Money::Currency id: usd>>
@example Mismatching currencies
'USD 2000'.to_money("EUR") #=> ArgumentError
@see Money.from_string