Float to binary

前端 未结 6 2184
醉话见心
醉话见心 2020-12-25 10:17

I\'m trying to convert a floating point number to binary representation; how can I achieve this? My goal is, however, not to be limited by 2m so I\'m hoping for something th

6条回答
  •  我在风中等你
    2020-12-25 10:42

    For floats there is built-in method hex().

    http://docs.python.org/library/stdtypes.html#float.hex

    It gives you the hexadecimal representation of a given number. And translation form hex to binary is trivial.

    For example:

    In [15]: float.hex(1.25)
    Out[15]: '0x1.4000000000000p+0'
    
    In [16]: float.hex(8.25)
    Out[16]: '0x1.0800000000000p+3'
    

提交回复
热议问题