Float to binary

前端 未结 6 2183
醉话见心
醉话见心 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 11:02

    There is one trick that i observed that we can do using simple string manipulations. I felt this method to be simpler than other methods that i came across.

    s = "1101.0101"
    s1, s2 = s.split(".")
    s1 = int(s1, 2)
    s2 = int(s2, 2)/(2**len(s2))
    x = s1+s2
    print(x)
    

    Output :

    13.3125
    

    Hope it will be helpful to someone.

提交回复
热议问题