Packages

Money functions for operations on and localization of a money data type with support for ISO 4217 currencies and ISO 24165 digial tokens (crypto currencies).

Retired package: Deprecated

Current section

Files

Jump to
ex_money lib money sigil.ex
Raw

lib/money/sigil.ex

defmodule Money.Sigil do
@doc ~S"""
Implements the sigil `~M` for Money
The lower case `~m` variant does not exist as interpolation and excape
characters are not useful for Money sigils.
## Example
import Money.Sigil
iex> ~M[1000]usd
#Money<:USD, 1000>
iex> ~M[1000.34]usd
#Money<:USD, 1000.34>
"""
def sigil_M(amount, [_, _, _] = currency) do
Money.new(to_decimal(amount), atomize(currency))
end
defp to_decimal(string) do
string
|> String.replace("_", "")
|> Decimal.new()
end
defp atomize(currency) do
currency
|> List.to_string()
|> validate_currency!
end
def validate_currency!(currency) do
case Money.validate_currency(currency) do
{:ok, currency} -> currency
{:error, {_exception, reason}} -> raise Money.UnknownCurrencyError, reason
end
end
end