ericmj / decimal

Arbitrary precision decimal arithmetic

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

to_string(:scientific) is broken

lukaszsamson opened this issue · comments

Is:

iex(15)> Decimal.new("1.34")|>Decimal.to_string(:scientific)                         
"1.34"
iex(16)> Decimal.new("0.34")|>Decimal.to_string(:scientific)
"0.34"
iex(17)> Decimal.new("1234")|>Decimal.to_string(:scientific)
"1234"
iex(18)> Decimal.new("1.234E-1")|>Decimal.to_string(:scientific)
"0.1234"

Should be:

iex(15)> Decimal.new("1.34")|>Decimal.to_string(:scientific)                         
"1.34E+0"
iex(16)> Decimal.new("0.34")|>Decimal.to_string(:scientific)
"3.4E-1"
iex(17)> Decimal.new("1234")|>Decimal.to_string(:scientific)
"1.234E+3"
iex(18)> Decimal.new("1.234E-1")|>Decimal.to_string(:scientific)
"1.234E-1"

We follow these conversion rules: http://speleotrove.com/decimal/daconvs.html#reftostr which are based on IEEE 754. Maybe we should a :strict_scientific option?

Good idea, no breaking changes this way.