Python program to convert float decimal to octal number

Octal Number uses eight digits, 0,1,2,3,4,5,6,7. Also called as base 8 number system. Each position in an octal number represents a 0 power of the base (8). Last position in an octal number represents a x power of the base (8).

Decimal number system has base 10 as it uses 10 digits from 0 to 9. In decimal number system, the successive positions to the left of the decimal point represent units, tens, hundreds, thousands, and so on.

Given a float decimal value and input the decimal places number, our task is to convert it to octal form. Let’s say we have the following floating-point number −


At first, we take the integer part from the floating-point value and convert it to octal then we take fractional part and convert it to octal form and lastly combine both. Consider decimal places 12. The output will be −


Convert floating to octal by taking user input


def float_convert_octal(my_number, places = 3): my_whole, my_dec = str(my_number).split(".") my_whole = int(my_whole) my_dec = int (my_dec) res = oct(my_whole).lstrip("0o") + "." for x in range(places): my_whole, my_dec = str((decimal_converter(my_dec)) * 8).split(".") my_dec = int(my_dec) res += my_whole return res def decimal_converter(num): while num > 1: num /= 10 return num n = input("Enter the floating point value : \n") p = int(input("Enter the number of decimal places of the result : \n")) print(float_convert_octal(n, places = p))


Enter the floating point value :
Enter the number of decimal places of the result :

Updated on: 12-Aug-2022


Kickstart Your Career

Get certified by completing the course

Get Started