Dinheiro v0.1.3 Dinheiro View Source

Documentation for Dinheiro.

Link to this section Summary

Types

t()

Type that represents Dinheiro struct with:

:quantia as integer
:moeda as atom that represents an ISO 4217 code

Functions

Compares two Dinheiro structs with each other. They must each be of the same moeda and then their value are compared

Example:

iex> Dinheiro.compare(Dinheiro.new(12345, :BRL), Dinheiro.new(12345, :BRL))
0
iex> Dinheiro.compare(Dinheiro.new(12345, :BRL), Dinheiro.new(12346, :BRL))
-1
iex> Dinheiro.compare(Dinheiro.new(12346, :BRL), Dinheiro.new(12345, :BRL))
1

Divide a Dinheiro struct by a positive integer value

Return a new Dinheiro structs with value multiplied by other value. The first parameter must be a struct of Dinheiro

Create a new Dinheiro struct using a default value of Moeda. The default currency can be set in the system Mix config

Create a new Dinheiro struct

Return a new Dinheiro structs with subtract of two values. The first parameter must be a struct of Dinheiro

Return a new Dinheiro structs with sum of two values. The first parameter must be a struct of Dinheiro

Return a float value from a Dinheiro structs

Link to this section Types

Link to this type t() View Source
t() :: %Dinheiro{moeda: atom(), quantia: integer()}
Type that represents Dinheiro struct with:
:quantia as integer
:moeda as atom that represents an ISO 4217 code

Link to this section Functions

Link to this function compare(a, b) View Source
compare(t(), t()) :: integer()

Compares two Dinheiro structs with each other. They must each be of the same moeda and then their value are compared

Example:

iex> Dinheiro.compare(Dinheiro.new(12345, :BRL), Dinheiro.new(12345, :BRL))
0
iex> Dinheiro.compare(Dinheiro.new(12345, :BRL), Dinheiro.new(12346, :BRL))
-1
iex> Dinheiro.compare(Dinheiro.new(12346, :BRL), Dinheiro.new(12345, :BRL))
1
Link to this function divide(a, b) View Source
divide(t(), integer() | [integer()]) :: [t()]

Divide a Dinheiro struct by a positive integer value

Example:

iex> Dinheiro.divide(Dinheiro.new(100, :BRL), 2)
[%Dinheiro{quantia: 5000, moeda: :BRL}, %Dinheiro{quantia: 5000, moeda: :BRL}]
iex> Dinheiro.divide(Dinheiro.new(101, :BRL), 2)
[%Dinheiro{quantia: 5050, moeda: :BRL}, %Dinheiro{quantia: 5050, moeda: :BRL}]

Divide a Dinheiro struct by an list of values that represents a division ratio.

Example:

iex> Dinheiro.divide(Dinheiro.new(0.05, :BRL), [3, 7])
[%Dinheiro{quantia: 2, moeda: :BRL}, %Dinheiro{quantia: 3, moeda: :BRL}]
Link to this function multiply(a, b) View Source
multiply(t(), integer() | float()) :: t()

Return a new Dinheiro structs with value multiplied by other value. The first parameter must be a struct of Dinheiro.

Example:

iex> Dinheiro.multiply(Dinheiro.new(2, :BRL), 2)
%Dinheiro{quantia: 400, moeda: :BRL}
iex> Dinheiro.multiply(Dinheiro.new(5, :BRL), 2.5)
%Dinheiro{quantia: 1250, moeda: :BRL}
iex> Dinheiro.multiply(Dinheiro.new(4, :BRL), -2)
%Dinheiro{quantia: -800, moeda: :BRL}

Create a new Dinheiro struct using a default value of Moeda. The default currency can be set in the system Mix config.

Example:

iex> Application.put_env(:ex_dinheiro, :default_moeda, :BRL)
  iex> Dinheiro.new(12345)
  %Dinheiro{quantia: 1234500, moeda: :BRL}
  iex> Dinheiro.new(123.45)
  %Dinheiro{quantia: 12345, moeda: :BRL}
Link to this function new(quantia, moeda) View Source
new(integer() | float(), atom() | String.t()) :: t()

Create a new Dinheiro struct.

Example:

iex> Dinheiro.new(12345, :BRL)
%Dinheiro{quantia: 1234500, moeda: :BRL}
iex> Dinheiro.new(12345, "BRL")
%Dinheiro{quantia: 1234500, moeda: :BRL}
iex> Dinheiro.new(123.45, :BRL)
%Dinheiro{quantia: 12345, moeda: :BRL}
iex> Dinheiro.new(123.45, "BRL")
%Dinheiro{quantia: 12345, moeda: :BRL}
Link to this function subtract(a, b) View Source
subtract(t(), t() | integer() | float()) :: t()

Return a new Dinheiro structs with subtract of two values. The first parameter must be a struct of Dinheiro.

Example:

iex> Dinheiro.subtract(Dinheiro.new(2, :BRL), Dinheiro.new(1, :BRL))
%Dinheiro{quantia: 100, moeda: :BRL}
iex> Dinheiro.subtract(Dinheiro.new(4, :BRL), 2)
%Dinheiro{quantia: 200, moeda: :BRL}
iex> Dinheiro.subtract(Dinheiro.new(5, :BRL), 2.5)
%Dinheiro{quantia: 250, moeda: :BRL}
iex> Dinheiro.subtract(Dinheiro.new(4, :BRL), -2)
%Dinheiro{quantia: 600, moeda: :BRL}
Link to this function sum(a, b) View Source
sum(t(), t() | integer() | float()) :: t()

Return a new Dinheiro structs with sum of two values. The first parameter must be a struct of Dinheiro.

Example:

iex> Dinheiro.sum(Dinheiro.new(1, :BRL), Dinheiro.new(1, :BRL))
%Dinheiro{quantia: 200, moeda: :BRL}
iex> Dinheiro.sum(Dinheiro.new(1, :BRL), 2)
%Dinheiro{quantia: 300, moeda: :BRL}
iex> Dinheiro.sum(Dinheiro.new(1, :BRL), 2.5)
%Dinheiro{quantia: 350, moeda: :BRL}
iex> Dinheiro.sum(Dinheiro.new(2, :BRL), -1)
%Dinheiro{quantia: 100, moeda: :BRL}
Link to this function to_float(from) View Source
to_float(t()) :: float()

Return a float value from a Dinheiro structs.

Example:

iex> Dinheiro.to_float(%Dinheiro{quantia: 200, moeda: :BRL})
2.0
iex> Dinheiro.to_float(Dinheiro.new(50.5, :BRL))
50.5
iex> Dinheiro.to_float(Dinheiro.new(-4, :BRL))
-4.0