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
- Convert String float to float list in Python
- Python - Convert Float String List to Float Values
- Python program to convert decimal to binary number
- Python program to convert hex string to decimal
- Python program to convert any base to decimal by using int() method
- Python | Convert tuple to float value
- Python - Convert Float to digit list
- Python | Convert Joint Float string to Numbers
- Python Program to Convert any Positive Real Number to Binary string
- Python - Summation of float string list
- Python | Ways to sort list of float values
- Python | List of float to string conversion
- Python - Filter float strings from String list
- Python | Average of Float Numbers
- Python - Produce K evenly spaced float values
- Python - Resolve Float Keys in Dictionary
- Python | Decimal to binary list conversion
- Python | Decimal step range in list
- Python - K length decimal Places
- Python program to convert time from 12 hour to 24 hour format
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.
Improved By : nidhi_biet