Elixir v1.1.0 Integer

Functions for working with integers.

Summary

Functions

Returns the ordered digits for the given non-negative integer

Converts a binary from a text representation of an integer in an optional base base to the corresponding integer

Returns a char list which corresponds to the text representation of the given integer

Returns a char list which corresponds to the text representation of the given integer in the given base

Returns a binary which corresponds to the text representation of some_integer

Returns a binary which corresponds to the text representation of some_integer in base base

Returns the integer represented by the ordered digits

Macros

Determines if an integer is even

Determines if an integer is odd

Functions

digits(n, base \\ 10)

Specs

digits(non_neg_integer, pos_integer) :: [non_neg_integer]

Returns the ordered digits for the given non-negative integer.

An optional base value may be provided representing the radix for the returned digits.

Examples

iex> Integer.digits(101)
[1, 0, 1]

iex> Integer.digits(58127, 2)
[1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
parse(binary, base \\ 10)

Specs

parse(binary, 2 .. 36) ::
  {integer, binary} |
  :error |
  no_return

Converts a binary from a text representation of an integer in an optional base base to the corresponding integer.

If the base base is not given, base 10 will be used.

If successful, returns a tuple of the form {integer, remainder_of_binary}. Otherwise :error.

Raises an error if base is less than 2 or more than 36.

Examples

iex> Integer.parse("34")
{34, ""}

iex> Integer.parse("34.5")
{34, ".5"}

iex> Integer.parse("three")
:error

iex> Integer.parse("34", 10)
{34, ""}

iex> Integer.parse("f4", 16)
{244, ""}

iex> Integer.parse("Awww++", 36)
{509216, "++"}

iex> Integer.parse("fab", 10)
:error

iex> Integer.parse("a2", 38)
** (ArgumentError) invalid base 38
to_char_list(number)

Specs

to_char_list(integer) :: char_list

Returns a char list which corresponds to the text representation of the given integer.

Inlined by the compiler.

Examples

iex> Integer.to_char_list(7)
'7'
to_char_list(number, base)

Specs

to_char_list(integer, 2 .. 36) :: char_list

Returns a char list which corresponds to the text representation of the given integer in the given base.

Inlined by the compiler.

Examples

iex> Integer.to_char_list(1023, 16)
'3FF'
to_string(some_integer)

Specs

to_string(integer) :: String.t

Returns a binary which corresponds to the text representation of some_integer.

Inlined by the compiler.

Examples

iex> Integer.to_string(123)
"123"
to_string(some_integer, base)

Specs

to_string(integer, 2 .. 36) :: String.t

Returns a binary which corresponds to the text representation of some_integer in base base.

Inlined by the compiler.

Examples

iex> Integer.to_string(100, 16)
"64"
undigits(digits, base \\ 10)

Specs

undigits([integer], integer) :: integer

Returns the integer represented by the ordered digits.

An optional base value may be provided representing the radix for the digits.

Examples

iex> Integer.undigits([1, 0, 1])
101

iex> Integer.undigits([1, 4], 16)
20

Macros

is_even(n)

Determines if an integer is even.

Returns true if n is an even number, otherwise false.

Allowed in guard clauses.

is_odd(n)

Determines if an integer is odd.

Returns true if n is an odd number, otherwise false.

Allowed in guard clauses.