DecimalEnv
This library provides two macros to be able to use Decimals with regular
Elixir operators i.e.
iex(1)> import DecimalEnv
iex(2)> decimal do
...(2)> 21.0 + "21.0"
...(2)> end
#Decimal<42.0>It is possible to provide a decimal context as argument for the macro i.e
iex(1)> import DecimalEnv
iex(2)> decimal [precision: 2] do
...(2)> 1 / 3
...(2)> end
#Decimal<0.33>For more information about context check
Decimal.Contextstruct inDecimalrepository.
Not all the Elixir statements are valid. The purpose of these macros is to
make it easier to write formulas involving Decimal type, so writing Elixir
code inside the decimal block is discouraged i.e.
iex(1)> import DecimalEnv
iex(2)> Enum.reduce([10.99, 10.01, 20.99, 0.01], 0.00,
...(2)> fn x, acc ->
...(2)> decimal do: x + acc
...(2)> end)
#Decimal<42.00>
The previous example calculates the sum of a list of Floats, but returns
the result as a Decimal type. The code is more clear than its equivalent
without the macro:
iex(1)> Enum.reduce([10.99, 10.01, 20.99, 0.01], Decimal.new(0.00),
...(2)> fn x, %Decimal{} = acc ->
...(2)> Decimal.add(Decimal.new(x), acc)
...(2)> end)
#Decimal<42.00>
The only statement offered is if statement i.e:
iex(1)> import DecimalEnv
iex(1)> a = 42.0
iex(3)> decimal do
...(3)> if a > 10, do: a, else: 10
...(3)> end
#Decimal<42.0>The guard in the
ifstatement should be a decimal comparison.
It is posible to assign variables inside the block, but pattern matching is not available i.e:
iex(1)> import DecimalEnv
iex(2)> decimal [precision: 2] do
...(2)> a = div(42.1, 2.0)
...(2)> a * 2
...(2)> end
#Decimal<42>Constant values inside the decimal block are precalculated at compile time.
Installation
Add it to your mix.exs
def deps do
[{:decimal_env, "~> 0.2"}]
endAuthor
Alexander de Sousa.
License
DecimalEnv is released under the MIT License. See the LICENSE file for
further details.