module Money::Parse

Defined in:

money/money/parse.cr

Instance Method Summary

Instance Method Detail

def parse(str : String, *, allow_ambiguous : Bool = true) : Money #

Creates a Money instance from a string, or returns nil on failure.

Money.parse?("10.00 USD") # => Money(@amount=10.0, @currency="USD")
Money.parse?("$10.00")    # => Money(@amount=10.0, @currency="USD")
Money.parse?("10.00")     # => nil

If allow_ambiguous is true (default), returns the first matching currency for ambiguous values, otherwise returns nil.

Money.parse?("$10.00", allow_ambiguous: true)  # => Money(@amount=10.0, @currency="USD")
Money.parse?("$10.00", allow_ambiguous: false) # => nil

Thousands separator and decimal mark are guessed from the string.

Money.parse?("$10.00") # => Money(@amount=10.0, @currency="USD")
Money.parse?("$10,00") # => Money(@amount=10.0, @currency="USD")

Money.parse?("$100,000") # => Money(@amount=100000.0, @currency="USD")
Money.parse?("$100.000") # => Money(@amount=100000.0, @currency="USD")

NOTE Raises Money::Parse::Error on failure instead of returning nil.


[View source]
def parse?(str : String, *, allow_ambiguous : Bool = true) : Money | Nil #

Creates a Money instance from a string, or returns nil on failure.

Money.parse?("10.00 USD") # => Money(@amount=10.0, @currency="USD")
Money.parse?("$10.00")    # => Money(@amount=10.0, @currency="USD")
Money.parse?("10.00")     # => nil

If allow_ambiguous is true (default), returns the first matching currency for ambiguous values, otherwise returns nil.

Money.parse?("$10.00", allow_ambiguous: true)  # => Money(@amount=10.0, @currency="USD")
Money.parse?("$10.00", allow_ambiguous: false) # => nil

Thousands separator and decimal mark are guessed from the string.

Money.parse?("$10.00") # => Money(@amount=10.0, @currency="USD")
Money.parse?("$10,00") # => Money(@amount=10.0, @currency="USD")

Money.parse?("$100,000") # => Money(@amount=100000.0, @currency="USD")
Money.parse?("$100.000") # => Money(@amount=100000.0, @currency="USD")

[View source]