/
decimal.ex
49 lines (45 loc) · 1.23 KB
/
decimal.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
defmodule Cldr.Decimal do
@moduledoc """
Adds a compatibility layer for functions which changed
either semantics or returns types between Decimal version
1.x and 2.x.
"""
decimal_version = Application.ensure_all_started(:decimal) &&
Application.spec(:decimal, :vsn)
|> List.to_string()
# To cater for both Decimal 1.x and 2.x
if Code.ensure_loaded?(Decimal) && function_exported?(Decimal, :normalize, 1) do
def reduce(decimal) do
Decimal.normalize(decimal)
end
else
def reduce(decimal) do
Decimal.reduce(decimal)
end
end
@spec compare(Decimal.t(), Decimal.t()) :: :eq | :lt | :gt
if Version.match?(decimal_version, "~> 1.6 or ~> 1.9.0-rc or ~> 1.9") do
def compare(d1, d2) do
Decimal.cmp(d1, d2)
end
else
def compare(d1, d2) do
Decimal.compare(d1, d2)
end
end
if Version.match?(decimal_version, "~> 2.0") do
def parse(string) do
case Decimal.parse(string) do
{decimal, ""} -> {decimal, ""}
_other -> {:error, string}
end
end
else
def parse(string) do
case Decimal.parse(string) do
{:ok, decimal} -> {decimal, ""}
_other -> {:error, string}
end
end
end
end