forked from jkvor/emysql
-
Notifications
You must be signed in to change notification settings - Fork 209
Open
Description
I have a table with float column(think it as a money balance column). For example,
CREATE TABLE account(..., balance float);
INSERT INTO account(..., balance) VALUES(..., 23.0);
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).
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels