Float to binary

前端 未结 6 2185
醉话见心
醉话见心 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:51

    If you want to convert a float to a string with d digits after the decimal radix point:

    1. Multiply the number by base**d.
    2. Round to the nearest integer.
    3. Convert the integer to a string.
    4. Insert the . character d digits before the end.

    For example, to represent 0.1 in base 12 with 4 decimal dozenal places,

    1. 0.1 × 124 = 2073.6
    2. Round to nearest integer → 2074
    3. Convert to string → 124A
    4. Add radix point → 0.124A

提交回复
热议问题