Integer
Functions for working with integers.
Summary
Functions
- digits(integer, base \\ 10)
-
Returns the ordered digits for the given non-negative
integer - is_even(integer)
-
Determines if an
integeris even - is_odd(integer)
-
Determines if
integeris odd - parse(binary, base \\ 10)
-
Parses a text representation of an integer
- to_charlist(integer)
-
Returns a charlist which corresponds to the text representation of the given
integer - to_charlist(integer, base)
-
Returns a charlist which corresponds to the text representation of
integerin the givenbase - to_string(integer)
-
Returns a binary which corresponds to the text representation of
integer - to_string(integer, base)
-
Returns a binary which corresponds to the text representation of
integerin the givenbase - undigits(digits, base \\ 10)
-
Returns the integer represented by the ordered
digits
Functions
digits(integer, base \\ 10)
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. This one can be an integer >= 2.
Examples
iex> Integer.digits(101) [1, 0, 1] iex> Integer.digits(170, 2) [1, 0, 1, 0, 1, 0, 1, 0]
is_even(integer) (macro)
Determines if an integer is even.
Returns true if the given integer is an even number, otherwise it returns false.
Allowed in guard clauses.
Examples
iex> Integer.is_even(10) true iex> Integer.is_even(5) false iex> Integer.is_even(-10) true iex> Integer.is_even(0) true
is_odd(integer) (macro)
Determines if integer is odd.
Returns true if the given integer is an odd number, otherwise it returns false.
Allowed in guard clauses.
Examples
iex> Integer.is_odd(5) true iex> Integer.is_odd(6) false iex> Integer.is_odd(-5) true iex> Integer.is_odd(0) false
parse(binary, base \\ 10)
parse(binary, 2..36) :: {integer, binary} | :error | no_return Parses a text representation of an integer.
An optional base to the corresponding integer can be provided. If base is not given, 10 will be used.
If successful, returns a tuple in the form of {integer, remainder_of_binary}. Otherwise :error.
Raises an error if base is less than 2 or more than 36.
If you want to convert a string-formatted integer directly to a integer, String.to_integer/1 or String.to_integer/2 can be used instead.
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_charlist(integer)
to_charlist(integer) :: charlist
Returns a charlist which corresponds to the text representation of the given integer.
Inlined by the compiler.
Examples
iex> Integer.to_charlist(123) '123' iex> Integer.to_charlist(+456) '456' iex> Integer.to_charlist(-789) '-789' iex> Integer.to_charlist(0123) '123'
to_charlist(integer, base)
to_charlist(integer, 2..36) :: charlist
Returns a charlist which corresponds to the text representation of integer in the given base.
base can be an integer between 2 and 36.
Inlined by the compiler.
Examples
iex> Integer.to_charlist(100, 16) '64' iex> Integer.to_charlist(-100, 16) '-64' iex> Integer.to_charlist(882681651, 36) 'ELIXIR'
to_string(integer)
to_string(integer) :: String.t
Returns a binary which corresponds to the text representation of integer.
Inlined by the compiler.
Examples
iex> Integer.to_string(123) "123" iex> Integer.to_string(+456) "456" iex> Integer.to_string(-789) "-789" iex> Integer.to_string(0123) "123"
to_string(integer, base)
to_string(integer, 2..36) :: String.t
Returns a binary which corresponds to the text representation of integer in the given base.
base can be an integer between 2 and 36.
Inlined by the compiler.
Examples
iex> Integer.to_string(100, 16) "64" iex> Integer.to_string(-100, 16) "-64" iex> Integer.to_string(882681651, 36) "ELIXIR"
undigits(digits, base \\ 10)
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. This one can be an integer >= 2.
Examples
iex> Integer.undigits([1, 2, 3]) 123 iex> Integer.undigits([1, 4], 16) 20 iex> Integer.undigits([]) 0
© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/elixir/1.3.4/Integer.html