Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FLOAT column becomes INT after being selected out #176

Open
arthurxie opened this issue Oct 24, 2015 · 0 comments
Open

FLOAT column becomes INT after being selected out #176

arthurxie opened this issue Oct 24, 2015 · 0 comments

Comments

@arthurxie
Copy link

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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant