Dinero
Elixir library for working with Money (slang Dinero)
How to use
ten_bucks = Dinero.new(10, :USD) #%Dinero{amount: 1000, currency: :USD}
twenty_bucks = Dinero.add(ten_bucks, 10) #%Dinero{amount: 2000, currency: :USD}
two_bucks = Dinero.subtract(twenty_bucks, 18) #%Dinero{amount: 200, currency: :USD}
four_bucks = Dinero.multiply(two_bucks, 2) #%Dinero{amount: 400, currency: :USD}
convert = Dinero.convert(four_bucks, :UAH, 26.2) #%Dinero{amount: 10480, currency: :UAH}Using with Postgres
Dinero.Ecto.DineroWithCurrency is an Ecto type that allows saving amount of dinero with currency to database.
-
Create custom Postrgres type
def up do execute "CREATE TYPE public.dinero_with_currency AS (amount integer, currency char(3))" end def down do execute "DROP TYPE public.dinero_with_currency" end -
Add column to the table
alter table (:some_table) do add :price, :dinero_with_currency end -
Add field to the schema
schema "some_table" do field :price, Dinero.Ecto.DineroWithCurrency end -
Save to DB:
iex> Repo.insert %Product{price: Dinero.new(20, :USD)} [debug] QUERY OK db=2.6ms decode=0.5ms queue=0.4ms INSERT INTO "products" ("price","inserted_at","updated_at") VALUES ($1,$2,$3) RETURNING "id" [{2000, "USD"}, ~N[2019-07-15 12:47:38], ~N[2019-07-15 12:47:38]] {:ok, %EctoCustomType.Product{ __meta__: #Ecto.Schema.Metadata<:loaded, "products">, id: 1, inserted_at: ~N[2019-07-15 12:47:38], price: %Dinero{amount: 2000, currency: :USD}, updated_at: ~N[2019-07-15 12:47:38] }} -
Get from DB:
iex> Repo.all(Product) [debug] QUERY OK source="products" db=1.2ms queue=1.3ms SELECT p0."id", p0."price", p0."inserted_at", p0."updated_at" FROM "products" AS p0 [] [ %EctoCustomType.Product{ __meta__: #Ecto.Schema.Metadata<:loaded, "products">, id: 1, inserted_at: ~N[2019-07-15 12:47:38], price: %Dinero{amount: 2000, currency: :USD}, updated_at: ~N[2019-07-15 12:47:38] } ]
Installation
If available in Hex, the package can be installed
by adding dinero to your list of dependencies in mix.exs:
def deps do
[
{:dinero, "~> 1.0.0"}
]
endDocumentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/dinero.
Authors
- Slava Fir (github profile), MLSDev
License
Dinero is released under the MIT license. See LICENSE for details.
About MLSDev
Dinero is maintained by MLSDev, Inc. We specialize in providing all-in-one solution in mobile and web development. Our team follows Lean principles and works according to agile methodologies to deliver the best results reducing the budget for development and its timeline.
Find out more here and don’t hesitate to contact us!