Python doesn’t provide any inbuilt method to easily convert floating point decimal numbers to binary number. So, Let’s do this manually.
To convert a floating point decimal number into binary, first convert the integer part into binary form and then fractional part into binary form and finally combine both results to get the final answer.
For Integer Part, keep dividing the number by 2 and noting down the remainder until and unless the dividend is less than 2. If so, stop and copy all the remainders together.
For Decimal Part, keep multiplying the decimal part with 2 until and unless 0 left as fractional part. After multiplying the first time, note down integral part and again multiply decimal part of the new value by 2. Keep doing this until reached a perfect number.
Above steps can be written as :
1(base 10) = 1(base 2) and .234(base 10) = .0011(base 2)
Now, to get the binary of 1.234, merge both results as a complete number.
(1)10 = (1)2[approx.]
(.234)10 = (.0011)2
(1.234)10 = (1.0011...)2
(1.234)10 = (1.0011)2
Below is the implementation :
Enter your floating point value : 1.234 Enter the number of decimal places of the result : 4 1.0011
Enter your floating point value : 11.234 Enter the number of decimal places of the result : 4 1011.0011
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Python program to represent floating number as hexadecimal by IEEE 754 standard
- Python regex | Check whether the input is Floating point number or not
- Floating point error in Python
- Floating Action type button in kivy - Python
- Animated Floating Action Button in kivy - Python
- Compute the natural logarithm of one plus each element in floating-point accuracy Using NumPy
- PyQt5 QSpinBox - Getting Pixel ratio as floating value
- PyQt5 QDockWidget – Setting Floating Property
- PyQt5 QDockWidget – Checking Floating Property
- Python program to convert decimal to binary number
- Python Program to Convert any Positive Real Number to Binary string
- Program to Convert Octal Number to Binary Number
- Python | Convert String to Binary
- Python | Ways to convert hex into binary
- Convert binary to string using Python
- Python - Convert Binary tuple to Integer
- Python program to convert time from 12 hour to 24 hour format
- Python | Program to convert String to a List
- Python program to convert float decimal to Octal number
- Python program to convert POS to SOP