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

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

I have binary strings in the form of either:

<<"5.7778345">>

or

<<"444555">>
         


        
4条回答
  •  温柔的废话
    2021-02-08 02:13

    This is the pattern that we use:

    binary_to_number(B) ->
        list_to_number(binary_to_list(B)).
    
    list_to_number(L) ->
        try list_to_float(L)
        catch
            error:badarg ->
                list_to_integer(L)
        end.
    

提交回复
热议问题