Skip to content

Commit

Permalink
Merge pull request elixirmoney#21 from andrewtimberlake/remove/mult_div
Browse files Browse the repository at this point in the history
Remove multiply/divide by Money
  • Loading branch information
liuggio committed May 25, 2016
2 parents 795879f + 97bd5f2 commit 816232e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 32 deletions.
18 changes: 4 additions & 14 deletions lib/money.ex
Original file line number Diff line number Diff line change
Expand Up @@ -270,41 +270,31 @@ defmodule Money do
do: subtract(m, round(subtractend * 100))
def subtract(a, b), do: fail_currencies_must_be_equal(a, b)

@spec multiply(t, t | integer | float) :: t
@spec multiply(t, integer | float) :: t
@doc ~S"""
Multiplies two `Money` together or a `Money` with an integer
Multiplies a `Money` by an amount
## Example:
iex> Money.multiply(Money.new(100, :USD), Money.new(10, :USD))
%Money{amount: 1000, currency: :USD}
iex> Money.multiply(Money.new(100, :USD), 10)
%Money{amount: 1000, currency: :USD}
iex> Money.multiply(Money.new(100, :USD), 1.5)
%Money{amount: 150, currency: :USD}
"""
def multiply(%Money{amount: a, currency: cur}, %Money{amount: b, currency: cur}),
do: Money.new(a * b, cur)
def multiply(%Money{amount: amount, currency: cur}, multiplier) when is_integer(multiplier),
do: Money.new(amount * multiplier, cur)
def multiply(%Money{amount: amount, currency: cur}, multiplier) when is_float(multiplier),
do: Money.new(round(amount * multiplier), cur)
def multiply(a, b), do: fail_currencies_must_be_equal(a, b)

@spec divide(t, t | integer) :: t
@spec divide(t, integer) :: t
@doc ~S"""
Divides one `Money` from another or a `Money` with an integer
Divides a `Money` by an amount
## Example:
iex> Money.divide(Money.new(100, :USD), Money.new(10, :USD))
%Money{amount: 10, currency: :USD}
iex> Money.divide(Money.new(100, :USD), 10)
%Money{amount: 10, currency: :USD}
"""
def divide(%Money{amount: a, currency: cur}, %Money{amount: b, currency: cur}),
do: Money.new(div(a, b), cur)
def divide(%Money{amount: amount, currency: cur}, divisor) when is_integer(divisor),
do: Money.new(div(amount, divisor), cur)
def divide(a, b), do: fail_currencies_must_be_equal(a, b)

@spec to_string(t, Keyword.t) :: String.t
@doc ~S"""
Expand Down
20 changes: 2 additions & 18 deletions test/money_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -120,31 +120,15 @@ defmodule MoneyTest do
end

test "test multiply" do
assert Money.multiply(Money.new(200, :USD), Money.new(100, :USD)) == Money.new(20000, :USD)
assert Money.multiply(Money.new(200, :USD), 100) == Money.new(20000, :USD)
assert Money.multiply(Money.new(200, :USD), 1.5) == Money.new(300, :USD)
assert Money.multiply(Money.new(200, :USD), 1.333) == Money.new(267, :USD)
assert Money.multiply(Money.new(200, :USD), 1.335) == Money.new(267, :USD)
end

test "multiply error" do
assert_raise ArgumentError, fn ->
Money.multiply(Money.new(124, :EUR), Money.new(123, :USD))
end
end

test "test divide" do
assert Money.divide(Money.new(200, :USD), Money.new(100, :USD)) == Money.new(2, :USD)
end

test "test divide2" do
assert Money.divide(Money.new(139, :USD), Money.new(113, :USD)) == Money.new(1, :USD)
end

test "divide error" do
assert_raise ArgumentError, fn ->
Money.divide(Money.new(124, :EUR), Money.new(123, :USD))
end
assert Money.divide(Money.new(200, :USD), 100) == Money.new(2, :USD)
assert Money.divide(Money.new(139, :USD), 113) == Money.new(1, :USD)
end

test "test to_string" do
Expand Down

0 comments on commit 816232e

Please sign in to comment.