DecimalEnv (DecimalEnv v1.0.0) View Source

This module provides macros to encapsulate Decimal operations while using Elixir arithmetic and comparison operators and functions.

Link to this section Summary

Types

Context.

Option.

Options.

Output type.

Type name.

Functions

Runs a block under the Decimal environment. All numeric values will be converted to Decimal.

Link to this section Types

Specs

context() :: nil | keyword() | Decimal.Context.t()

Context.

Specs

option() :: {:as, type_name()} | {:context, context()}

Option.

Specs

options() :: [option()]

Options.

Specs

output() :: integer() | float() | binary() | Decimal.t()

Output type.

Specs

type_name() ::
  nil | :decimal | :float | :integer | :string | :scientific | :xsd | :raw

Type name.

Link to this section Functions

Link to this macro

decimal(options \\ [], list)

View Source (macro)

Specs

decimal(options(), Macro.t()) :: Macro.t()

Runs a block under the Decimal environment. All numeric values will be converted to Decimal.

Options:

  • :context - Decimal.Context struct. it can also be a keyword list.
  • :as - In which type we should have the result. The possible values are the following:
    • :decimal (default)
    • :float
    • :integer
    • :string
    • :xsd
    • :raw
    • :scientific

Examples

iex> import DecimalEnv
iex> decimal do: 42.42
#Decimal<42.42>
iex> decimal do: 84.0 - 21 - "21"
#Decimal<42.0>
iex> decimal as: :scientific do
...>   "0.0000000002" + 0.000000004
...> end
"4.2E-9"
iex> decimal context: [precision: 2, rounding: :ceiling], as: :integer do
...>   21.1 + 20
...> end
42