Skip to content

Commit

Permalink
Merge pull request #29 from seantanly/fix-compare
Browse files Browse the repository at this point in the history
fix(Cldr.Unit.compare): Units built from Decimal string value are not compared corrrectly
  • Loading branch information
kipcole9 authored Aug 3, 2022
2 parents 2e9c311 + 962e916 commit 6a67e18
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 1 addition & 5 deletions lib/cldr/unit/math.ex
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,7 @@ defmodule Cldr.Unit.Math do
end

def compare(%Unit{unit: unit} = unit_1, %Unit{unit: unit} = unit_2) do
cond do
unit_1.value == unit_2.value -> :eq
unit_1.value > unit_2.value -> :gt
unit_1.value < unit_2.value -> :lt
end
Ratio.compare(Ratio.new(unit_1.value), Ratio.new(unit_2.value))
end

def compare(%Unit{} = unit_1, %Unit{} = unit_2) do
Expand Down
6 changes: 6 additions & 0 deletions test/conversion_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ defmodule Cldr.Unit.Conversion.Test do
end
end

test "compare/2 for [Decimal] units that are built from string" do
s_unit = Cldr.Unit.new!(Decimal.new("300.0"), "gram")
t_unit = Cldr.Unit.new!(Decimal.new("60.0"), "kilogram")
assert Cldr.Unit.compare(s_unit, t_unit) == :lt
end

test "convert!/2" do
assert MyApp.Cldr.Unit.convert!(MyApp.Cldr.Unit.new!(:foot, 3), :meter)

Expand Down

0 comments on commit 6a67e18

Please sign in to comment.