DecimalEnv (DecimalEnv v1.0.1)

Copy Markdown View Source

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

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.

Types

context()

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

Context.

option()

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

Option.

options()

@type options() :: [option()]

Options.

output()

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

Output type.

type_name()

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

Type name.

Functions

decimal(options \\ [], list)

(macro)
@spec 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.new("42.42")
iex> decimal do: 84.0 - 21 - "21"
Decimal.new("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