Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir-将字符串解析为浮动导致错误_Elixir - Fatal编程技术网

Elixir-将字符串解析为浮动导致错误

Elixir-将字符串解析为浮动导致错误,elixir,Elixir,尝试将字符串解析为浮点时出现以下错误: case insert_product_shop(conn, existing_product.id, existing_shop.id, String.to_float(posted_product["price"])) do 错误: 20:45:29.766 [error] #PID<0.342.0> running Api.Router terminated Server: 172.20.10.2:4000 (http) Request

尝试将字符串解析为浮点时出现以下错误:

case insert_product_shop(conn, existing_product.id, existing_shop.id, String.to_float(posted_product["price"])) do
错误:

20:45:29.766 [error] #PID<0.342.0> running Api.Router terminated
Server: 172.20.10.2:4000 (http)
Request: POST /products
** (exit) an exception was raised:
    ** (ArgumentError) argument error
        :erlang.binary_to_float(58.25)
        (api) lib/api/router.ex:120: anonymous fn/1 in Api.Router.do_match/4
        (api) lib/api/router.ex:1: Api.Router.plug_builder_call/2
        (api) lib/plug/debugger.ex:123: Api.Router.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) /Users/Ben/Development/Projects/vepo/api/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.exe
cute/4
我得到:

20:54:12.769 [error] #PID<0.336.0> running Api.Router terminated
Server: 172.20.10.2:4000 (http)
Request: POST /products
** (exit) an exception was raised:
    ** (ArgumentError) argument error
        :erlang.binary_to_float(24)
        (api) lib/api/router.ex:82: anonymous fn/1 in Api.Router.do_match/4
        (api) lib/api/router.ex:1: Api.Router.plug_builder_call/2
        (api) lib/plug/debugger.ex:123: Api.Router.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) /Users/Ben/Development/Projects/vepo/api/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.exe
cute/4
20:54:12.769[错误]#运行Api的PID。路由器终止
服务器:172.20.10.2:4000(http)
请求:员额/产品
**(退出)引发了一个异常:
**(ArgumentError)参数错误
:erlang.binary_to_float(24)
(api)lib/api/router.ex:82:api.router.do_match/4中的匿名fn/1
(api)lib/api/router.ex:1:api.router.plug\u builder\u call/2
(api)lib/plug/debugger.ex:123:api.Router.call/2
(plug)lib/plug/adapters/cowboy/handler.ex:15:plug.adapters.cowboy.handler.upgrade/4
(cowboy)/Users/Ben/Development/Projects/vepo/api/deps/cowboy/src/cowboy_protocol.erl:442::cowboy_protocol.exe
可爱的/4

如何正确地将字符串解析为float?我还可以允许它是字符串还是浮点,如果是字符串,则解析为浮点?

根据错误,输入看起来已经是一个
浮点

** (ArgumentError) argument error
    :erlang.binary_to_float(58.25)
事实上,
String.to_float/1
会在输入不是
字符串时引发错误

iex(1)> String.to_float(58.25)
** (ArgumentError) argument error
    :erlang.binary_to_float(58.25)
iex(1)> String.to_float("58.25")
58.25
还请注意,如果输入没有十进制数字,则
String.to_float/1
也会发出抱怨

iex(2)> String.to_float("58")
** (ArgumentError) argument error
    :erlang.binary_to_float("58")
iex(2)> String.to_float("58.0")
58.0
您需要编写一个自定义函数。我不确定
发布的产品[“价格”]
来自何处,也不确定您是否期望它具有不同的输入类型。这可能是一个错误

一种可能的解决方法是始终将输入强制转换为
String
,并使用
Float.parse/1

iex(12)> {f, _} = Float.parse(to_string("58.0"))
{58.0, ""}
iex(13)> f
58.0
iex(14)> {f, _} = Float.parse(to_string(58.0))
{58.0, ""}
iex(15)> f
58.0
请注意,
Float.parse/1
可能返回一个
:错误
,因此您必须处理它

另一种可能更有效的方法是使用
is_float/1
is_string/1
仅在必要时处理转换

defmodule Price do
  def to_float(input) when is_float(input) do
    input
  end
  def to_float(input) do
    case Float.parse(to_string(input)) do
      {f, ""} -> f
      _       -> :error
    end
  end
end

iex(2)> Price.to_float(32)
32.0
iex(3)> Price.to_float(32.0)
32.0
iex(4)> Price.to_float("32")
32.0

根据错误,看起来输入已经是一个
浮点值

** (ArgumentError) argument error
    :erlang.binary_to_float(58.25)
事实上,
String.to_float/1
会在输入不是
字符串时引发错误

iex(1)> String.to_float(58.25)
** (ArgumentError) argument error
    :erlang.binary_to_float(58.25)
iex(1)> String.to_float("58.25")
58.25
还请注意,如果输入没有十进制数字,则
String.to_float/1
也会发出抱怨

iex(2)> String.to_float("58")
** (ArgumentError) argument error
    :erlang.binary_to_float("58")
iex(2)> String.to_float("58.0")
58.0
您需要编写一个自定义函数。我不确定
发布的产品[“价格”]
来自何处,也不确定您是否期望它具有不同的输入类型。这可能是一个错误

一种可能的解决方法是始终将输入强制转换为
String
,并使用
Float.parse/1

iex(12)> {f, _} = Float.parse(to_string("58.0"))
{58.0, ""}
iex(13)> f
58.0
iex(14)> {f, _} = Float.parse(to_string(58.0))
{58.0, ""}
iex(15)> f
58.0
请注意,
Float.parse/1
可能返回一个
:错误
,因此您必须处理它

另一种可能更有效的方法是使用
is_float/1
is_string/1
仅在必要时处理转换

defmodule Price do
  def to_float(input) when is_float(input) do
    input
  end
  def to_float(input) do
    case Float.parse(to_string(input)) do
      {f, ""} -> f
      _       -> :error
    end
  end
end

iex(2)> Price.to_float(32)
32.0
iex(3)> Price.to_float(32.0)
32.0
iex(4)> Price.to_float("32")
32.0

根据错误消息,您已经有一个浮点。只需使用
posted\u product[“price”]
。根据错误消息,您已经有了一个浮动。只需使用
发布的产品[“价格”]