# `ExUtils.Numbers`
[🔗](https://github.com/sheharyarn/ex_utils/blob/main/lib/ex_utils/numbers.ex#L2)

Utility functions to work with numeric types.

# `parseable`

```elixir
@type parseable() :: t() | String.t() | nil
```

A value that can be parsed into a numeric type

# `t`

```elixir
@type t() :: number() | Decimal.t()
```

Any numeric value including Decimal

# `is_numeric`
*macro* 

Checks whether a value is an integer, float, or decimal.

You need to `import` or `require` this module to use this guard.

## Examples

```elixir
iex> is_numeric(12)
true

iex> is_numeric(Decimal.new("12.5"))
true

iex> is_numeric("12.5")
false
```

# `is_positive`
*macro* 

Checks whether a value is a positive integer.

You need to `import` or `require` this module to use this guard.

## Examples

```elixir
iex> is_positive(12)
true

iex> is_positive(0)
false

iex> is_positive(1.5)
false
```

# `to_decimal`

```elixir
@spec to_decimal(parseable()) :: Decimal.t() | nil
```

Parse a string or another numeric type to decimal.

If the input is nil or invalid string, returns nil.

Also see `to_decimal!/1`.

## Examples

```elixir
iex> ExUtils.Numbers.to_decimal(" 12.5 ")
Decimal.new("12.5")

iex> ExUtils.Numbers.to_decimal(12)
Decimal.new("12")

iex> ExUtils.Numbers.to_decimal("invalid")
nil
```

# `to_decimal!`

```elixir
@spec to_decimal!(parseable()) :: Decimal.t()
```

Same as `to_decimal/1` but raises when unable to parse.

## Examples

```elixir
iex> ExUtils.Numbers.to_decimal!("12.5")
Decimal.new("12.5")

iex> ExUtils.Numbers.to_decimal!("invalid")
** (ArgumentError) unable to parse "invalid"
```

# `to_float`

```elixir
@spec to_float(parseable()) :: float() | nil
```

Parse a string or another numeric type to a float.

If the input is nil or invalid string, returns nil.

Also see `to_float!/1`.

## Examples

```elixir
iex> ExUtils.Numbers.to_float(" 12.5 ")
12.5

iex> ExUtils.Numbers.to_float(12)
12.0

iex> ExUtils.Numbers.to_float("invalid")
nil
```

# `to_float!`

```elixir
@spec to_float!(parseable()) :: float()
```

Same as `to_float/1` but raises when unable to parse.

## Examples

```elixir
iex> ExUtils.Numbers.to_float!("12.5")
12.5

iex> ExUtils.Numbers.to_float!("invalid")
** (ArgumentError) unable to parse "invalid"
```

# `to_integer`

```elixir
@spec to_integer(parseable()) :: integer() | nil
```

Parse a string or another numeric type to an integer.

If the input is a decimal or float, it rounds it.
If the input is nil or invalid string, returns nil.

Also see `to_integer!/1`.

## Examples

```elixir
iex> ExUtils.Numbers.to_integer(Decimal.new("12.5"))
13

iex> ExUtils.Numbers.to_integer(12.4)
12

iex> ExUtils.Numbers.to_integer("12")
12

iex> ExUtils.Numbers.to_integer("12.5")
nil
```

# `to_integer!`

```elixir
@spec to_integer!(parseable()) :: integer()
```

Same as `to_integer/1` but raises when unable to parse.

## Examples

```elixir
iex> ExUtils.Numbers.to_integer!("12")
12

iex> ExUtils.Numbers.to_integer!("12.5")
** (ArgumentError) unable to parse "12.5"
```

# `trunc`

```elixir
@spec trunc(t()) :: integer()
```

Truncates a numeric value and returns an integer.

## Examples

```elixir
iex> ExUtils.Numbers.trunc(Decimal.new("5.7"))
5

iex> ExUtils.Numbers.trunc(-5.7)
-5

iex> ExUtils.Numbers.trunc(5)
5
```

---

*Consult [api-reference.md](api-reference.md) for complete listing*
