Python doesn’t support any inbuilt function to easily convert floating point decimal numbers to octal representation. Let’s do this manually.
Approach : To convert a decimal number having fractional part into octal, first convert the integer part into octal form and then fractional part into octal form and finally combine the two results to get the final answer.
For Integer Part, Keep dividing the number by 8 and noting down the remainder until and unless the dividend is less than 8 and copy all the remainders together.
For the Decimal Part, Keep multiplying the decimal part with 8 until and unless we get 0 left as fractional part. After multiplying the first time, note down an integral part and then again multiply the decimal part of new value by 8 and keep doing this until perfect number is reached.
Above steps can be written as :
7(base 10) = 7(base 8) and .16(base 10) = .1217(base 8)
Now, to get the octal of the decimal number 7.16, merge the two octal results.
(7)10 = (7)8
(0.16)10 = (0.1217...)8
So, (7.16)10 = (7.1217...)8
or, (7.16)10 = (7.1217)8 (approx. value)
Below is the implementation :
Enter your floating point value : 7.16 Enter the number of decimal places of the result : 10 7.1217273146
Enter your floating point value : 7.1234 Enter the number of decimal places of the result : 5 7.07713
- Python program to convert hex string to decimal
- Python program to covert decimal to binary number
- Quickly convert Decimal to other bases in Python
- Python program to convert POS to SOP
- Python program to convert floating to binary
- Python | Program to convert String to a List
- Python | Program to convert a tuple to a string
- Python program to convert a list to string
- Python | Convert number to list of integers
- float() in Python
- Python program to convert camel case string to snake case
- Python program to convert seconds into hours, minutes and seconds
- Python program to convert time from 12 hour to 24 hour format
- Python | Check for float string
- Python | sympy.Float() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.