- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to convert floating to binary
In this article, we will see how to convert floating-point value to binary. Binary uses two digits, 0 and 1. Also called as base 2 number system Each position in a binary number represents a 0 power of the base (2). Last position in a binary number represents a x power of the base (2).
First, we take the integer part from the floating point value and convert it to binary then take fractional part and convert it to binary form and lastly combining both.
Let’s say we have the following floating point number −
22.625
Convert decimal 22 to binary 10110. Convert decimal 0.625 to binary 0.101. Combine integer and fraction to get the following output −
10110.101
Convert floating to binary by taking user input
Example
def floatoctal_convert(my_number, places = 3): my_whole, my_dec = str(my_number).split(".") my_whole = int(my_whole) my_dec = int (my_dec) res = bin(my_whole).lstrip("0b") + "." for x in range(places): my_whole, my_dec = str((my_decimal_converter(my_dec)) * 8).split(".") my_dec = int(my_dec) res += my_whole return res def my_decimal_converter(num): while num > 1: num /= 10 return num # Driver Code n = input("Enter floating point value : \n") p = int(input("Enter the number of decimal places of the result : \n")) print(floatoctal_convert(n, places = p))
Output
Enter floating point value : 2.34 Enter the number of decimal places of the result : 3 10.256
- Related Articles
- C# program to convert floating to binary
- Java program to convert floating to binary
- Python program to convert decimal to binary number
- Python Program to Convert Gray Code to Binary
- Python Program to Convert Binary to Gray Code
- Convert decimal to binary number in Python program
- C# Program to Convert Binary to Decimal
- 8085 Program to convert ASCII to binary
- 8085 program to convert gray to binary
- Swift Program to convert Decimal to Binary
- Swift Program to convert Binary to Decimal
- JAVA Program to Convert Binary to Octal
- JAVA Program to Convert Octal to Binary
- JAVA program to Convert Hexadecimal to Binary
- Golang Program to convert Binary to Octal

Advertisements