I have binary strings in the form of either:
<<"5.7778345">>
or
<<"444555">>
I do not know before hand whether it will be a float or integer.
I tried doing a check to see if it is an integer. This does not work since it is binary. I alos tried converting binary to list, then check if int or float. I did not have much success with that.
It needs to be a function such as:
binToNumber(Bin) ->
%% Find if int or float
Return.
Anyone have a good idea of how to do this?
4条答案
按热度按时间daolsyd01#
No quick way to do it. Use something like this instead:
This should convert the binary to a list (string), then try to fit it in a float. When that can't be done, we return an integer. Otherwise, we keep the float and return that.
rjzwgtxy2#
这是我们使用的模式:
qkf9rpyu3#
由于Erlang/OTP R16 B:
c9x0cxw04#
binary_to_term
函数及其对应的term_to_binary
可能会很好地为您服务。