# Monetized
[![Build Status](](
[![Inline docs](](

As a general rule, using floats to store money is a [bad idea](

Enter Monetized. A library that aims to facilitate the handling of money through a safe way to store currency values and
and providing abstractions to perform arithmetical operations on those values.

Monetized leverages [Decimal]('s ability to safely handle arbitrary precision to perform calculations
on money values.

A typical `%Monetized.Money{}` struct will contain a value in the shape of a `Decimal` struct and a currency (if any) as a string.

## Notice

This library is currently under active development and as such you
should be aware the the API may or may not change drastically.

## Usage

Monetized also ships with a `Ecto.Type` that gives us an easier way to store money.


alias Monetized.Money

schema "transaction" do
  field :description, :string
  field :balance, Money, default:



By doing this we're able to pass a `%Money{}` struct to the changeset and
insert in the `Repo` since Ecto knows how to convert that struct into a primitive
type to save and back when you read from it.


balance = Money.make("£ 100.50")
changeset = Transaction.changeset(%Transaction{}, %{description: "Invoice payment", balance: balance})

# Or

balance = %{value: "100.50", currency: "GBP"}
changeset = Transaction.changeset(%Transaction{}, %{description: "Invoice payment", balance: balance})


Check the [docs]( for more.

## Other usage

alias Monetized.Money
alias Monetized.Math

# Create with a string
iex> item_one = Money.make("£ 200.50")

# Or a float
iex> item_two = Money.make(10.25, [currency: "GBP"])

# Adding two moneys together
iex> Math.add(item_one, item_two)

# Or an integer
iex> balance = Money.make(100_000, [currency: "USD"])

# Substracting from money (currency inferred from balance)
iex> result = Math.sub(balance, 50_000)

# Getting the string representation
iex> Money.to_string(result, [show_currency: true])
"$ 50,000.00"

# You can also use `from_integer/2`, `from_float/2`, `from_decimal/2` and `from_string/2`
# respectively if the desired behavior is to raise when the amount is 
# not of the expected type.

# If either the symbol or the currency key is found within the string,
# that currency will be used. However, if a different currency is given in the
# options, it will take precedence.

iex> Money.from_string("£ 200")

iex> Money.from_string("200 EUR")

iex> Money.from_integer(200)

iex> Money.from_float(200.50)

iex> decimal =
...> Money.from_decimal(decimal, currency: "EUR")

iex> Money.from_integer("10")
** (FunctionClauseError) no function clause matching in Monetized.Money.from_integer/2
    (monetized) lib/money.ex:204: Monetized.Money.from_integer("10", [])


Check the [docs]( for more examples

## Config

config :monetized, config: [
  delimiter: ",",
  separator: ".",
  currency: "USD",
  format: "%c %n%s%d"

## Installation

  Add monetized to your list of dependencies in `mix.exs`:

def deps do
  [{:monetized, "~> 0.3.0"}]



- [x] Refactor fractional values to use Decimal
- [x] Add Ecto.Type
- [ ] Add more currencies (currenctly only supports USD, GBP and EUR)
- [ ] Get feedback...

## Contributing

See []( for details.

## Licensing

See [](