elixirmoney / money

Elixir library for working with Money safer, easier, and fun... Is an interpretation of the Fowler's Money pattern in fun.prog.
https://hex.pm/packages/money/
MIT License
827 stars 141 forks source link

division by float #83

Closed namxam closed 1 year ago

namxam commented 6 years ago

Is there a reason why division are only supported by integers? If not, I would try to fix it and submit a pull request.

andrewtimberlake commented 6 years ago

Division of money returns an array of equal amounts with one holding the remainder.

iex> Money.divide(Money.new(9, :USD), 4)
[
  %Money{amount: 3, currency: :USD},
  %Money{amount: 2, currency: :USD},
  %Money{amount: 2, currency: :USD},
  %Money{amount: 2, currency: :USD}
]

How do you propose Money.divide(Money.t, float) would work?

namxam commented 6 years ago

Ok, sorry, then I did not read the documentation correctly. But this seems to be extremely counterintuitive. I have never seen a division to be implemented like this.

To be honest I would expect this function to be named like "split" or something similar. Because add, multiply, and subtract are implemented just like their basic arithmetic operation counterparts.

andrewtimberlake commented 6 years ago

When you work with money, the literal stuff, how do you divide it?

When you add money, you take two amounts and you have a new amount When you subtract money, you remove an amount and you are left with a new amount When you invest and multiply your money, you increase it and are left with a new amount But when you divide your money up, you are left with multiple amounts.

namxam commented 6 years ago

Yeah, but actually isn't it the case for any value? It just feels really unnatural. I probably would have gone with a different verb … like split. I am not a native speaker, but I think you also say split a bill and not divide a bill.

jnmandal commented 5 years ago

For what its worth we had a similar discussion in my company. We use the divide function as a split, and we were surprised to see that it supported the use case of dividing by a negative amount.

BobbieBarker commented 3 years ago

What about calculating percentages with division?