Skip to content

Commit

Permalink
Add Money.integer?/1
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Apr 28, 2023
1 parent eee1c2f commit 2fd1a22
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,15 @@
# Changelog

## Money v5.14.0

This is the changelog for Money v5.14.0 released on April 29th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags)

**Note** `ex_money 5.13.0` is supported on Elixir 1.11 and later versions only. It also requires `ex_cldr_numbers 2.31` or later.

### Enhancements

* Adds `Money.integer?/1` to return a boolean indicatng if a money amount is an integer value (ie has no significant fractional digits).

## Money v5.13.0

This is the changelog for Money v5.13.0 released on April 28th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags)
Expand Down
38 changes: 38 additions & 0 deletions lib/money.ex
Expand Up @@ -2214,6 +2214,44 @@ defmodule Money do
end
end

@doc """
Returns a boolean indicating if `t:Money.t/0`
is an integer value (has no significant fractional
digits).
## Arguments
* `money` is any valid `t:Money.t/0` type returned
by `Money.new/2`
## Example
iex> Money.integer?(Money.new(:USD, 0))
true
iex> Money.integer?(Money.new(:USD, "1.10"))
false
"""
Cldr.Macros.doc_since("5.10.0")
@spec integer?(Money.t()) :: boolean

if function_exported?(Decimal, :integer?, 1) do
def integer?(%{amount: amount}) do
Decimal.integer?(amount)
end
else
def integer?(%{amount: %Decimal{coef: :NaN}}), do: false
def integer?(%{amount: %Decimal{coef: :inf}}), do: false
def integer?(%{amount: %Decimal{coef: coef, exp: exp}}), do: exp >= 0 or zero_after_dot?(coef, exp)

defp zero_after_dot?(coef, exp) when coef >= 10 and exp < 0,
do: Kernel.rem(coef, 10) == 0 and zero_after_dot?(Kernel.div(coef, 10), exp + 1)

defp zero_after_dot?(coef, exp),
do: coef == 0 or exp == 0
end

@doc """
Returns a boolean indicating if `t:Money.t/0`
has a positive value.
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
@@ -1,7 +1,7 @@
defmodule Money.Mixfile do
use Mix.Project

@version "5.13.0"
@version "5.14.0"

def project do
[
Expand Down

0 comments on commit 2fd1a22

Please sign in to comment.