# Decimal v1.9.0 Decimal.Context View Source

The context is kept in the process dictionary. It can be accessed with `get/0` and `set/1`.

The default context has a precision of 28, the rounding algorithm is `:half_up`. The set trap enablers are `:invalid_operation` and `:division_by_zero`.

## Fields

• `precision` - maximum number of decimal digits in the coefficient. If an operation result has more digits it will be rounded to `precision` digits with the rounding algorithm in `rounding`.
• `rounding` - the rounding algorithm used when the coefficient's number of exceeds `precision`. Strategies explained below.
• `flags` - a list of signals that for which the flag is sent. When an exceptional condition is signalled its flag is set. The flags are sticky and will be set until explicitly cleared.
• `traps` - a list of set trap enablers for signals. When a signal's trap enabler is set the condition causes `Decimal.Error` to be raised.

## Rounding algorithms

• `:down` - round toward zero (truncate). Discarded digits are ignored, result is unchanged.
• `:half_up` - if the discarded digits is greater than or equal to half of the value of a one in the next left position then the coefficient will be incremented by one (rounded up). Otherwise (the discarded digits are less than half) the discarded digits will be ignored.
• `:half_even` - also known as "round to nearest" or "banker's rounding". If the discarded digits is greater than half of the value of a one in the next left position then the coefficient will be incremented by one (rounded up). If they represent less than half discarded digits will be ignored. Otherwise (exactly half), the coefficient is not altered if it's even, or incremented by one (rounded up) if it's odd (to make an even number).
• `:ceiling` - round toward +Infinity. If all of the discarded digits are zero or the sign is negative the result is unchanged. Otherwise, the coefficient will be incremented by one (rounded up).
• `:floor` - round toward -Infinity. If all of the discarded digits are zero or the sign is positive the result is unchanged. Otherwise, the sign is negative and coefficient will be incremented by one.
• `:half_down` - if the discarded digits is greater than half of the value of a one in the next left position then the coefficient will be incremented by one (rounded up). Otherwise (the discarded digits are half or less) the discarded digits are ignored.
• `:up` - round away from zero. If all discarded digits are zero the coefficient is not changed, otherwise it is incremented by one (rounded up).

This table shows the results of rounding operations for all the rounding algorithms:

Rounding algorithm5.52.51.61.11.0-1.0-1.1-1.6-2.5-5.5
`:up`63221-1-2-2-3-6
`:down`52111-1-1-1-2-5
`:ceiling`63221-1-1-1-2-5
`:floor`52111-1-2-2-3-6
`:half_up`63211-1-1-2-3-6
`:half_down`52211-1-1-2-2-5
`:half_even`62211-1-1-2-2-6

# Link to this section Summary

## Functions

Gets the process' context.

Set the process' context.

Update the process' context.

Runs function with given context.

# t()

View Source

## Specs

```t() :: %Decimal.Context{
flags: [Decimal.signal()],
precision: pos_integer(),
rounding: Decimal.rounding(),
traps: [Decimal.signal()]
}```

# get()

View Source (since 1.9.0)

## Specs

`get() :: t()`

Gets the process' context.

# set(context)

View Source (since 1.9.0)

## Specs

`set(t()) :: :ok`

Set the process' context.

# update(fun)

View Source (since 1.9.0)

## Specs

`update((t() -> t())) :: :ok`

Update the process' context.

`with(t(), (() -> x)) :: x when x: var`