decimal_arithmetic v0.1.3 DecimalArithmetic

Module extends embedded arithmetic with decimal one. If at least one operand of operation is of type Decimal.t the second one is promoted to Decimal struct too.

Examples

iex> a = ~m(98.01)
#Decimal<98.01>
iex> b = ~m(10.01)
#Decimal<10.01>
iex> c = a * b
#Decimal<981.0801>
iex> d = c / 77
#Decimal<12.7413>
iex> (a + b * c / d) * 3.14
#Decimal<2727.9692>
iex> net_price = ~m(34.78)
#Decimal<34.78>
iex> vat_rate = 23
23
iex> net_price * (1 + vat_rate / 100) |> Decimal.round(2)
#Decimal<42.78>

Link to this section Summary

Functions

Returns true if two decimable are not equal.

Multiplies decimables or delegates multiplication to Kernel module.

Adds two decimables or delegate addition to Kernel module.

Subtracts two decimables or delegates subtraction to Kernel module.

Divides two decimables or delegates division to Kernel module.

Compares two decimables or delegates comparison to Kernel module.

Compares two decimables.

Returns true if two decimable are equal or delegates equality to Kernel module.

Compares two decimables or delegates comparison to Kernel module.

Compares two decimables.

Casts string literal to Decimal.t.

Link to this section Types

Link to this type

decimable()
decimable() :: number() | Decimal.t()

Link to this section Functions

Returns true if two decimable are not equal.

Examples

iex> 3.15 != ~m(3.15)
false
iex> 1.00001 != ~m(1.00002)
true

Multiplies decimables or delegates multiplication to Kernel module.

Examples

iex> 7 * ~m(2.33)
#Decimal<16.31>

Adds two decimables or delegate addition to Kernel module.

Examples

iex> ~m(1) + 3.1415
#Decimal<4.1415>
iex> 1 + 3
4

Subtracts two decimables or delegates subtraction to Kernel module.

Examples

iex> 3.19 - ~m(5.45)
#Decimal<-2.26>
iex> 3.20 - 5.45
-2.25

Divides two decimables or delegates division to Kernel module.

Examples

iex> ~m(3) / 4
#Decimal<0.75>
iex> 3 / 4
0.75

Compares two decimables or delegates comparison to Kernel module.

## Examples

iex> 3 < ~m(2)
false
iex> ~m(3) < 5
true
iex> ~m(2.21) < ~m(2.20)
false

Compares two decimables.

Examples

iex> 3 <= ~m(2)
false
iex> ~m(3) <= 3
true
iex> ~m(2.20) <= ~m(2.21)
true

Returns true if two decimable are equal or delegates equality to Kernel module.

Examples

iex> ~m(3.15) == 3.15
true
iex> ~m(5.304) == 5.304
true
iex> ~m(1.00001) == ~m(1.00002)
false

Compares two decimables or delegates comparison to Kernel module.

Examples

iex> 3 > ~m(2)
true
iex> ~m(3) > 5
false
iex> ~m(2.21) > ~m(2.20)
true

Compares two decimables.

Examples

iex> 3 >= ~m(2)
true
iex> ~m(3) >= 3
true
iex> ~m(2.20) >= ~m(2.21)
false
Link to this function

sigil_m(string, list)

Casts string literal to Decimal.t.

Examples

iex> ~m[89.01]
#Decimal<89.01>
iex> ~m{34.34}
#Decimal<34.34>
iex> ~m(1)
#Decimal<1>