You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The client calls web API to get the account detail, which returns a XML with the balance column formatted as: io_lib:format("~.2f", [Balance]). And the process will crash because the Balance is not a float.
I just checked the code and found the to_float function does not convert Num to float:
to_float(Data) ->
{ok, [Num], _Leftovers} = case io_lib:fread("~f", binary_to_list(Data)) of
% note: does not need conversion
{error, _} ->
case io_lib:fread("~d", binary_to_list(Data)) of % note: does not need conversion
{ok, [_], []} = Res ->
Res;
{ok, [X], E} ->
io_lib:fread("~f", lists:flatten(io_lib:format("~w~s~s" ,[X,".0",E])))
end
;
Res ->
Res
end,
Num.
As you see, some times a column will be transferred as int number text and parsed as io_lib:fread("~d", binary_to_list(Data)). So, I think this function should be like this:
to_float(Data) ->
{ok, [Num], _Leftovers} = case io_lib:fread("~f", binary_to_list(Data)) of
% note: does not need conversion
{error, _} ->
case io_lib:fread("~d", binary_to_list(Data)) of % note: does not need conversion
{ok, [_], []} = Res ->
Res;
{ok, [X], E} ->
io_lib:fread("~f", lists:flatten(io_lib:format("~w~s~s" ,[X,".0",E])))
end
;
Res ->
Res
end,
float(Num).
The text was updated successfully, but these errors were encountered:
I have a table with float column(think it as a money balance column). For example,
The client calls web API to get the account detail, which returns a XML with the balance column formatted as:
io_lib:format("~.2f", [Balance]).
And the process will crash because theBalance
is not a float.I just checked the code and found the to_float function does not convert Num to float:
As you see, some times a column will be transferred as int number text and parsed as
io_lib:fread("~d", binary_to_list(Data))
. So, I think this function should be like this:The text was updated successfully, but these errors were encountered: