toftis
toftis

Reputation: 1080

Get a single float value from a string in elixir

Is there a method that takes in a float string and converts it to a single float. It must also handle strings without . like "1"

String.to_float does not handle "1"

iex(5)> String.to_float("1")
** (ArgumentError) argument error
:erlang.binary_to_float("1")
iex(5)> String.to_float("1.0")
1.0

Float.parse handles "1", but returns a tuple.

iex(4)> Float.parse("1")
{1.0, ""}

Upvotes: 1

Views: 724

Answers (1)

Artyom Malyshev
Artyom Malyshev

Reputation: 44

Convert Elixir string to integer or float

Maybe you should use something like

{res, _} = Float.parse("1")

or

elem Float.parse("1"), 0

Upvotes: 3

Related Questions