Python program to convert floating to binary



Floating number is a number that has a decimal point and can represent both large and very small values. Binary number is a number expressed in the base-2 numeral system, using 0's and 1's.

The conversion of floating-point number to binary representation in Python requires to represent the number in the IEEE 754 format(a set of representation of numerical values and symbols).

For example, consider a floating number 9.87, its IEEE 754 32-bit binary representation (1 bit for the sign, 8 bits for exponent, and 23 bits for the significand) is "01000001000111011110101110000101".

In this article, we will discuss different ways to convert floating to binary using Python along with examples -

Using struct Module

The struct module in Python is used to convert between Python values and C-style binary data. This module allows us to convert floating point number to binary by packing it into IEEE 754 format using struct.pack() function and then to interpret it as an integer using struct.unpack().

Example

In the example program below, we will use the struct module to convert floating to binary format -

import struct

n = 9.87
output = struct.unpack('!I', struct.pack('!f', n))[0]
print(f"{output:032b}")
#032b formats output as a 32-bit binary string

Note: The !f packs a float as 32-bit big-endian and !I unpacks those 4 bytes as a 32-bit big endian unsigned integer.

The output returned by the above code is as follows -

01000001000111011110101110000101

Using NumPy

NumPy is an open-source Python library, which supports multi-dimensional arrays (matrices) and provides a wide range of mathematical functions for array operations.

NumPy allows us to convert floating-point numbers to binary using the following -

  • np.float32() : This method is used to create a single-precision(32 bit) floating-point number.
  • np.view : This method allows us to interpret the memory of floating-point number as an integer.
  • bin() : This built-in function is used to get binary representation.

Example

In the example program below, we will use the Numpy library to convert floating number to binary representation -

import numpy as np

# Example floating-point number
n = 9.87

float_array = np.float32(n)
int_repr = float_array.view(np.int32)

# Convert to binary string
output = bin(int_repr)
print(output)

The output returned by the above code is as follows -

0b1000001000111011110101110000101

Using IEEE 754 Bit Manipulation

In this method, we will manually extract the sign, exponent and mantissa of the floating-point number and convert it to binary IEEE 754 representation.

Example

In the example program below, we will extract the sign bit and split the number into integer and fractional parts, further the integer part is converted using bin() function and the fractional part is converted by multiplying by 2 and extracting bits -

n = 9.87
#If n is greater than 0, sign is assigned as 0 else as 1
sign = '0' if n >= 0 else '1'
n= abs(n)

# Getting integer and fractional parts
int_part = int(n)
frac_part = n - int_part

# convert integer part to binary
int_bin = bin(int_part)[2:]

# convert fractional part to binary
frac_bin = []
while frac_part and len(frac_bin) < 23:
    frac_part *= 2
    bit = int(frac_part)
    frac_bin.append(str(bit))
    frac_part -= bit

# normalize
exponent = len(int_bin) - 1
mantissa = int_bin[1:] + ''.join(frac_bin)

# adjust mantissa to 23 bits
mantissa = (mantissa + '0' * 23)[:23]

# exponent with bias (127)
exponent_bin = f"{exponent + 127:08b}"

# IEEE 754 Binary Representation
output = sign + exponent_bin + mantissa
print(output)

The output returned by the above code is as follows -

01000001000111011110101110000101
Updated on: 2025-06-09T14:26:50+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements