Skip to content

Commit

Permalink
Bump version and update changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Apr 16, 2019
1 parent ce94000 commit f4eea90
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Changelog for Cldr v3.4.2

This is the changelog for Cldr v3.4.2 released on April 16th, 2019. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/cldr/tags)

### Bug Fixes

* `Money.put_fraction/2` now correctly allows setting the fraction to 0.

### Enhancements

* `Money.round/2` allows setting `:currency_digits` to an integer number of digits in addition to the options `:iso`, `:cash` and `:accounting`. The default remains `:iso`.

* Improves the documentation for `Money.to_string/2`.

# Changelog for Cldr v3.4.1

This is the changelog for Cldr v3.4.1 released on April 5th, 2019. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/cldr/tags)
Expand Down
30 changes: 17 additions & 13 deletions lib/money.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1238,10 +1238,9 @@ defmodule Money do
as "banker's rounding"
* `:currency_digits` which determines the rounding increment.
The valid options are `:cash`, `:accounting` and `:iso`. The
default is `:iso`. The rounding increment applies to currencies
such as :AUD and :CHF which have an accounting increment of 0.01
but a minimum cash increment of 0.05.
The valid options are `:cash`, `:accounting` and `:iso` or
an integer value representing the rounding factor. The
default is `:iso`.
## Notes
Expand All @@ -1251,14 +1250,18 @@ defmodule Money do
3. Apply an appropriate rounding increment. Most currencies
round to the same precision as the number of decimal digits, but some
such as :AUD and :CHF round to a minimum such as 0.05 when its a cash
amount.
such as `:CHF` round to a minimum such as `0.05` when its a cash
amount. The rounding increment is applied when the option
`:currency_digits` is set to `:cash`
## Examples
iex> Money.round Money.new("123.73", :CHF), currency_digits: :cash
#Money<:CHF, 123.75>
iex> Money.round Money.new("123.73", :CHF), currency_digits: 0
#Money<:CHF, 124>
iex> Money.round Money.new("123.7456", :CHF)
#Money<:CHF, 123.75>
Expand Down Expand Up @@ -1298,8 +1301,8 @@ defmodule Money do
currency.cash_digits
end

defp digits_from_opts(currency, _) do
currency.iso_digits
defp digits_from_opts(_currency, digits) when is_integer(digits) do
digits
end

defp round_to_nearest(%Money{currency: code} = money, opts) do
Expand Down Expand Up @@ -1359,31 +1362,32 @@ defmodule Money do
The fraction can only be set if it matches the number of
decimal digits for the currency associated with the `money`.
Therefore, for a currency with 2 decimal digits, the
maximum for `fraction` is `99`.
## Examples
iex> Money.put_fraction Money.new(:USD, "2.49"), 99
#Money<:USD, 2.99>
iex> Money.put_fraction Money.new(:USD, "2.49"), 0
#Money<:USD, 2.0>
iex> Money.put_fraction Money.new(:USD, "2.49"), 999
{:error,
{Money.InvalidAmountError, "Rounding up to 999 is invalid for currency :USD"}}
"""
def put_fraction(money, fraction \\ 0)

def put_fraction(%Money{} = money, 0) do
money
end

@one Decimal.new(1)
@zero Decimal.new(0)

def put_fraction(%Money{currency: code, amount: amount}, upto) when is_integer(upto) do
with {:ok, currency} <- Currency.currency_for_code(code) do
digits = currency.digits
diff = Decimal.from_float((100 - upto) * :math.pow(10, -digits))
if Decimal.cmp(diff, @one) == :lt && Decimal.cmp(@zero, diff) == :lt do
if Decimal.cmp(diff, @one) in [:lt, :eq] && Decimal.cmp(@zero, diff) in [:lt, :eq] do
new_amount =
Decimal.round(amount, 0)
|> Decimal.add(@one)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Money.Mixfile do
use Mix.Project

@version "3.4.1"
@version "3.4.2"

def project do
[
Expand Down

0 comments on commit f4eea90

Please sign in to comment.