Convert float decimal to Octal number 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 float decimal to octal number in python?”.

Python doesn’t support any inbuilt method to convert the same. Therefore, we need to do it manually.

Python Program to Convert float decimal to Octal number

 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((dec_converter(dec)) * 2).split(".") 
 dec = int(dec) 
 res += whole 
 return res 
 def dec_converter(num): 
 while num > 1: 
 num /= 10
 return num 
 n = input("Please enter floating point value : \n") 
 p = int(input("Please enter the number of decimal places : \n")) 
 print(float_bin(n, places = p)) 

Leave a Comment

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