Convert floating to binary in Python

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

In this Python program, we will have a look at “How to convert floating point decimal number to binary number?

Python Program to Convert floating to binary

 def float_bin(number, places = 3): 
 whole, dec = str(number).split(".") 
 whole = int(whole) 
 dec = int (dec) 
 res = bin(whole).lstrip("0b") + "."
 for x in range(places): 
 whole, dec = str((decimal_converter(dec)) * 2).split(".") 
 dec = int(dec) 
 res += whole 
 return res 
def decimal_converter(num): 
 while num > 1: 
 num /= 10
 return num 
n = input("Please enter floating value : \n") 
p = int(input("Please enter the number of decimal places: \n")) 
print(float_bin(n, places = p)) 


Please enter floating value  : 
Please enter the number of decimal places : 


Leave a Comment

Your email address will not be published. Required fields are marked *