将Elixir字符串转换为整数或浮点数

时间:2014-03-22 10:55:08

标签: elixir

我需要将字符串转换为浮点值或整数。没有像

这样的方法
string_to_integer

8 个答案:

答案 0 :(得分:124)

答案 1 :(得分:43)

除了José建议的Integer.parse/1Float.parse/1功能外,您还可以检查String.to_integer/1String.to_float/1

提示:另请参阅to_atom/1to_char_list/1to_existing_atom/1进行其他转换。

答案 2 :(得分:21)

感谢此页面上的相关人员,只需简化答案:

{intVal, ""} = Integer.parse(val)

感谢@dimagog的评论

答案 3 :(得分:10)

您可以将其转换为char_list,然后使用Erlang to_integer/1to_float/1

E.g。

iex> {myInt, _} = :string.to_integer(to_char_list("23"))
{23, []}

iex> myInt
23

答案 4 :(得分:10)

有4个函数可以从字符串

创建数字
  • String.to_integer,String.to_float
  • Integer.parse,Float.parse

String.to_integer效果很好,但String.to_float更难:

iex()> "1 2 3 10 100" |> String.split |> Enum.map(&String.to_integer/1)
[1, 2, 3, 10, 100]

iex()> "1.0 1 3 10 100" |> String.split |> Enum.map(&String.to_float/1)
** (ArgumentError) argument error
    :erlang.binary_to_float("1")
    (elixir) lib/enum.ex:1270: Enum."-map/2-lists^map/1-0-"/2
    (elixir) lib/enum.ex:1270: Enum."-map/2-lists^map/1-0-"/2

由于String.to_float只能处理格式良好的浮点数,例如:1.0,而不是1(整数)。这已记录在String.to_float的文档

  

返回其文本表示为字符串的float。

     

string必须是包含小数点的float的字符串表示形式。为了解析没有小数点的字符串作为浮点数,应该使用Float.parse / 1。否则,将引发ArgumentError。

但是Float.parse会返回一个包含2个元素的元组,而不是你想要的数字,所以把它放到管道中并不是"很酷":

iex()> "1.0 1 3 10 100" |> String.split \
|> Enum.map(fn n -> {v, _} = Float.parse(n); v end)

[1.0, 1.0, 3.0, 10.0, 100.0]

使用elem从元组中获取第一个元素,使其更短更甜:

iex()> "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]

答案 5 :(得分:3)

使用Test web app for your latest code.的问题是,只要它在尾端,就会解析字符串的任何非数字部分。例如:

Integer.parse/1

同样Integer.parse("01") # {1, ""} Integer.parse("01.2") # {1, ".2"} Integer.parse("0-1") # {0, "-1"} Integer.parse("-01") # {-1, ""} Integer.parse("x-01") # :error Integer.parse("0-1x") # {0, "-1x"} 具有以下结果:

String.to_integer/1

相反,首先验证字符串。

String.to_integer("01") # 1
String.to_integer("01.2") # ** (ArgumentError) argument error :erlang.binary_to_integer("01.2")
String.to_integer("0-1") # ** (ArgumentError) argument error :erlang.binary_to_integer("01.2")
String.to_integer("-01") # -1
String.to_integer("x-01") # ** (ArgumentError) argument error :erlang.binary_to_integer("01.2")
String.to_integer("0-1x") # ** (ArgumentError) argument error :erlang.binary_to_integer("01.2")

正则表达式可能更简单(例如re = Regex.compile!("^[+-]?[0-9]*\.?[0-9]*$") Regex.match?(re, "01") # true Regex.match?(re, "01.2") # true Regex.match?(re, "0-1") # false Regex.match?(re, "-01") # true Regex.match?(re, "x-01") # false Regex.match?(re, "0-1x") # false ),具体取决于您的使用案例。

答案 6 :(得分:0)

如果您想将字符串转换为字符串中的任何数字类型&删除所有其他字符,这可能是矫枉过正,但是如果它是一个浮点数则返回一个浮点数,如果它是一个int则返回一个int或如果该字符串不包含数字类型则返回nil。

@spec string_to_numeric(binary()) :: float() | number() | nil
def string_to_numeric(val) when is_binary(val), do: _string_to_numeric(Regex.replace(~r{[^\d\.]}, val, ""))
defp _string_to_numeric(val) when is_binary(val), do: _string_to_numeric(Integer.parse(val), val)
defp _string_to_numeric(:error, _val), do: nil
defp _string_to_numeric({num, ""}, _val), do: num
defp _string_to_numeric({num, ".0"}, _val), do: num
defp _string_to_numeric({_num, _str}, val), do: elem(Float.parse(val), 0)

答案 7 :(得分:0)

Decimal.new("1") |> Decimal.to_integer
Decimal.new("1.0") |> Decimal.to_float
相关问题