Elixir v1.1.0 Float

Functions for working with floating point numbers.

Summary

Functions

Rounds a float to the largest integer greater than or equal to num

Rounds a float to the largest integer less than or equal to num

Parses a binary into a float

Rounds a floating point value to an arbitrary number of fractional digits (between 0 and 15)

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

Returns a list which corresponds to the text representation of the given float

Returns a binary which corresponds to the text representation of the given float

Returns a binary which corresponds to the text representation of float

Functions

ceil(number, precision \\ 0)

Specs

ceil(float, 0 .. 15) :: float

Rounds a float to the largest integer greater than or equal to num.

ceil/2 also accepts a precision to round a floating point value down to an arbitrary number of fractional digits (between 0 and 15).

This function always returns floats. Kernel.trunc/1 may be used instead to truncate the result to an integer afterwards.

Examples

iex> Float.ceil(34.25)
35.0

iex> Float.ceil(-56.5)
-56.0

iex> Float.ceil(34.253, 2)
34.26
floor(number, precision \\ 0)

Specs

floor(float, 0 .. 15) :: float

Rounds a float to the largest integer less than or equal to num.

floor/2 also accepts a precision to round a floating point value down to an arbitrary number of fractional digits (between 0 and 15).

This function always returns a float. Kernel.trunc/1 may be used instead to truncate the result to an integer afterwards.

Examples

iex> Float.floor(34.25)
34.0

iex> Float.floor(-56.5)
-57.0

iex> Float.floor(34.253, 2)
34.25
parse(binary)

Specs

parse(binary) :: {float, binary} | :error

Parses a binary into a float.

If successful, returns a tuple of the form {float, remainder_of_binary}; otherwise, :error.

If a float formatted string wants to be directly converted to a float, String.to_float/1 can be used instead.

Examples

iex> Float.parse("34")
{34.0, ""}

iex> Float.parse("34.25")
{34.25, ""}

iex> Float.parse("56.5xyz")
{56.5, "xyz"}

iex> Float.parse("pi")
:error
round(number, precision \\ 0)

Specs

round(float, 0 .. 15) :: float

Rounds a floating point value to an arbitrary number of fractional digits (between 0 and 15).

This function only accepts floats and always returns a float. Use Kernel.round/1 if you want a function that accepts both floats and integers and always returns an integer.

Examples

iex> Float.round(5.5674, 3)
5.567

iex> Float.round(5.5675, 3)
5.568

iex> Float.round(-5.5674, 3)
-5.567

iex> Float.round(-5.5675, 3)
-5.568
to_char_list(float)

Specs

to_char_list(float) :: char_list

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

Inlined by the compiler.

Examples

iex> Float.to_char_list(7.0)
'7.00000000000000000000e+00'
to_char_list(float, options)

Specs

to_char_list(float, list) :: char_list

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

Options

  • :decimals - number of decimal points to show
  • :scientific - number of decimal points to show, in scientific format
  • :compact - when true, use the most compact representation (ignored with the scientific option)

Examples

iex> Float.to_char_list 7.1, [decimals: 2, compact: true]
'7.1'
to_string(float)

Specs

to_string(float) :: String.t

Returns a binary which corresponds to the text representation of the given float.

Inlined by the compiler.

Examples

iex> Float.to_string(7.0)
"7.00000000000000000000e+00"
to_string(float, options)

Specs

to_string(float, list) :: String.t

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

Options

  • :decimals - number of decimal points to show
  • :scientific - number of decimal points to show, in scientific format
  • :compact - when true, use the most compact representation (ignored with the scientific option)

Examples

iex> Float.to_string 7.1, [decimals: 2, compact: true]
"7.1"