Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for canonical XSD representation on to_string/2 #91

Merged
merged 2 commits into from Aug 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/decimal.ex
Expand Up @@ -1128,6 +1128,7 @@ defmodule Decimal do

* `:scientific` - number converted to scientific notation.
* `:normal` - number converted without a exponent.
* `:xsd` - number converted to the [canonical XSD representation](https://www.w3.org/TR/xmlschema-2/#decimal).
* `:raw` - number converted to its raw, internal format.

"""
Expand Down Expand Up @@ -1206,6 +1207,26 @@ defmodule Decimal do
IO.iodata_to_binary(str)
end

def to_string(%Decimal{} = decimal, :xsd) do
decimal |> canonical_xsd() |> to_string(:normal)
end

defp canonical_xsd(%Decimal{coef: 0} = decimal), do: %{decimal | exp: -1}

defp canonical_xsd(%Decimal{coef: coef, exp: 0} = decimal),
do: %{decimal | coef: coef * 10, exp: -1}

defp canonical_xsd(%Decimal{coef: coef, exp: exp} = decimal)
when exp > 0,
do: canonical_xsd(%{decimal | coef: coef * 10, exp: exp - 1})

defp canonical_xsd(%Decimal{coef: coef} = decimal)
when Kernel.rem(coef, 10) != 0,
do: decimal

defp canonical_xsd(%Decimal{coef: coef, exp: exp} = decimal),
do: canonical_xsd(%{decimal | coef: Kernel.div(coef, 10), exp: exp + 1})

@doc """
Returns the decimal represented as an integer.

Expand Down
27 changes: 26 additions & 1 deletion test/decimal_test.exs
Expand Up @@ -559,6 +559,31 @@ defmodule DecimalTest do
assert Decimal.to_string(~d"-inf", :raw) == "-Infinity"
end

test "to_string xsd" do
assert Decimal.to_string(~d"0", :xsd) == "0.0"
assert Decimal.to_string(~d"0.0", :xsd) == "0.0"
assert Decimal.to_string(~d"0.001", :xsd) == "0.001"
assert Decimal.to_string(~d"-0", :xsd) == "-0.0"
assert Decimal.to_string(~d"-1", :xsd) == "-1.0"
assert Decimal.to_string(~d"-0.00", :xsd) == "-0.0"
assert Decimal.to_string(~d"1.00", :xsd) == "1.0"
assert Decimal.to_string(~d"1000", :xsd) == "1000.0"
assert Decimal.to_string(~d"1000.000000", :xsd) == "1000.0"
assert Decimal.to_string(~d"12345.000", :xsd) == "12345.0"
assert Decimal.to_string(~d"42", :xsd) == "42.0"
assert Decimal.to_string(~d"42.42", :xsd) == "42.42"
assert Decimal.to_string(~d"0.42", :xsd) == "0.42"
assert Decimal.to_string(~d"0.0042", :xsd) == "0.0042"
assert Decimal.to_string(~d"010.020", :xsd) == "10.02"
assert Decimal.to_string(~d"-1.23", :xsd) == "-1.23"
assert Decimal.to_string(~d"-0.0123", :xsd) == "-0.0123"
assert Decimal.to_string(~d"1E+2", :xsd) == "100.0"
assert Decimal.to_string(~d"-42E+3", :xsd) == "-42000.0"
assert Decimal.to_string(~d"nan", :xsd) == "NaN"
assert Decimal.to_string(~d"-nan", :xsd) == "-NaN"
assert Decimal.to_string(~d"-inf", :xsd) == "-Infinity"
end

test "to_integer" do
Decimal.with_context(%Context{precision: 36, rounding: :floor}, fn ->
assert Decimal.to_integer(~d"0") == 0
Expand Down Expand Up @@ -914,7 +939,7 @@ defmodule DecimalTest do
end

test "issue #82" do
to_float = fn binary -> Decimal.new(binary) |> Decimal.to_float end
to_float = fn binary -> Decimal.new(binary) |> Decimal.to_float() end
assert to_float.("0.8888888888888888888888") == 0.8888888888888888888888
assert to_float.("0.9999999999999999") == 0.9999999999999999
assert to_float.("0.99999999999999999") == 0.99999999999999999
Expand Down