Thursday, June 18, 2009

What is the string representation for the float?

The Erlang people hold on to the point where the "123456" is for integer 123456 only, not for 123456.0 float! I was using nice and small pgsql PostgreSQL driver for Erlang and everything worked cool before I had troubles to fetch float datatype values which are rounded to integer.

As it happens, the list_to_float("123") will produce an error:

21> erlang:list_to_float("123").
** exception error: bad argument
in function list_to_float/1
called as list_to_float("123")
22> erlang:list_to_float("123.0").

That feature was explicitly confirmed by Erlang team, so we have to supply the missing ".0" ourselves. I had to patch the pgsql_util:decode_col locally to make it work:

decode_col(#desc{format=text, type=Float}, Value)
when Float =:= float4; Float =:= float8 ->
ListValue = binary_to_list(Value),
IsFloat = string:str(ListValue,"."),
IsFloat > 0 -> FValue = Value;
true -> FValue = Value ++ ".0"


Christian said...

Did you fork it on github so I can pull it?

METP said...

you can find it here