Can anyone please tell me how can I convert this float number: 12.25 to binary? I know how to convert the "12" but not the 0.25
Any help is much appreciated. Thanks
The sign of a binary floating-point number is represented by a single bit. A 1 bit indicates a negative number, and a 0 bit indicates a positive number. Before a floating-point binary number can be stored correctly, its mantissa must be normalized.
Python doesn't provide any inbuilt method to easily convert floating point decimal numbers to binary number.
Consider below example
We will consider the integer and fractional part separately.
The integral part is easy, 2 = 10.
For the fractional part:
0.625 × 2 = 1.25 1 Generate 1 and continue with the rest. 0.25 × 2 = 0.5 0 Generate 0 and continue. 0.5 × 2 = 1.0 1 Generate 1 and nothing remains.
So 0.625 = 0.101, and 2.625 = 10.101.
See this link for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With