For some reason Decimal module rounds numbers with many decimal places, even though the precision is set to 28. For example, IO.inspect(Decimal.new(3.65975555555452921))
returns #Decimal<3.659755555554529>
. This is my Decimal context:
%Decimal.Context{ flags: [], precision: 28, rounding: :half_up, traps: [:invalid_operation, :division_by_zero] }
Hi @charliekelly. The issue isn’t that Decimal is rounding, it’s that the float is rounding itself prior to being passed to the Decimal.new function:
iex(1)> 3.65975555555452921
3.659755555554529
Decimal.new with a string containing all of the desired characters works as expected;
iex(3)> Decimal.new("3.65975555555452921")
#Decimal<3.65975555555452921>
8 Likes
I suspect you may be on quite an old version of Decimal since Decimal.new/1
with a float is deprecated. For example:
iex> Decimal.new 12.345
warning: passing float to Decimal.new/1 is deprecated as floats have inherent inaccuracy. Use Decimal.from_float/1 or Decimal.cast/1 instead
4 Likes