ERLANG - binary string to integer or float -
i have binary strings in form of either:
<<"5.7778345">>
or
<<"444555">>
i not know before hand whether float or integer.
i tried doing check see if integer. not work since binary. , tried converting binary list check if int or float. not success that.
it needs function such as
bintonumber(bin) -> %%find if int or float return.
anyone have idea of how this?
all best
no quick way it. use instead:
bin_to_num(bin) -> n = binary_to_list(bin), case string:to_float(n) of {error,no_float} -> list_to_integer(n); {f,_rest} -> f end.
this should convert binary list (string), try fit in float. when can't done, return integer. otherwise, keep float , return that.
Comments
Post a Comment