# [erlang-questions] Float to binary

Alexey Romanov <>
Mon Mar 21 15:25:51 CET 2011

```> It seems to
> know that F is 1.0 as it let me rebind it.

1) All variables in list and binary comprehension generators are fresh, so
<<F:64/float>> <= ... does not rebind F at all.

> If I have a float value. This happens to be a frequency in MHz which I
> need to convert to a 32 bit integer big-endian format in Hz how do I get
> the float into a suitable format.

Why do you want to convert it to binary at all, instead of simply
`round(F*1000000)`?

But if you do (e.g. you want to pack multiple floats there),

1> <<1.0:64/float>>.
<<63,240,0,0,0,0,0,0>>

Yours, Alexey Romanov

On Mon, Mar 21, 2011 at 5:09 PM, Bob Cowdery <> wrote:
> If I have a float value. This happens to be a frequency in MHz which I
> need to convert to a 32 bit integer big-endian format in Hz how do I get
> the float into a suitable format.  The following converts the term into
> an external term format which is no good for the binary comprehension.
> How can I convert the value into a binary without getting the external
> 1> F=1.0.
> 1.0
> 2> B = term_to_binary(F, [{minor_version, 1}]).
> <<131,70,63,240,0,0,0,0,0,0>>
> 3> << <<(round(F*1000000)):32/big>> || <<F:64/float>> <=
> <<63,240,0,0,0,0,0,0>> >>.
> <<0,15,66,64>>
>
> Thanks once again for any help.
>
> Bob
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
>
>
```