How to convert a binary string to an integer or a float?

后端 未结 4 1772
梦谈多话
梦谈多话 2021-02-08 01:56

I have binary strings in the form of either:

<<"5.7778345">>

or

<<"444555">>
         


        
4条回答
  •  再見小時候
    2021-02-08 02:14

    Intermediate conversion to list is unnecessary since Erlang/OTP R16B:

    -spec binary_to_number(binary()) -> float() | integer().
    binary_to_number(B) ->
        try binary_to_float(B)
        catch
            error:badarg -> binary_to_integer(B)
        end.
    

提交回复
热议问题