ericmj / decimal

Arbitrary precision decimal arithmetic

Home Page:https://hexdocs.pm/decimal/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Context precision

bjuretic opened this issue · comments

iex(13)> D.set_context(%D.Context{D.get_context | precision: 4})

iex(14)> D.div(D.new(1), D.new(144.529793))
#Decimal<0.006919>

The precision option does not seem to count the zeros on the right of the decimal point, but it does count positive integers after them:

iex(42)> D.div(D.new(1), D.new(1334.5297923233))
#Decimal<0.0007493>

iex(43)> D.set_context(%D.Context{D.get_context | precision: 6})

iex(44)> D.div(D.new(1), D.new(1334.5297923233))
#Decimal<0.000749328>

This is correct, it is the precision of a calculation – not the number of digits, see https://en.wikipedia.org/wiki/Precision_(computer_science).

You can think of it as the number of digits used when expressing the number in scientific notation https://en.wikipedia.org/wiki/Scientific_notation.

If you want a specific number of digits you should round the number.