logo

Elixir Data Types

Convert string to integer or float

# returns tuple e.g. {123.45, ""}
Integer.parse(n)
Float.parse(n)

# returns integer
String.to_integer(n)
String.to_float(n)


Decimal.new(n) |> Decimal.to_integer
Decimal.new(n) |> Decimal.to_float

Example

"1.0 1 3 10 100" |> String.split |> Enum.map(fn n -> Float.parse(n) |> elem(0) end)
[1.0, 1.0, 3.0, 10.0, 100.0]

Convert float to string

n |> to_string([decimals: 2, compact: true])

Convert float to charlist

n |> to_charlist()


Icon Icon