Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert Int to Float in Python

  • 13 Mar, 2025
  • Com 0
How to Convert an Integer to Float in Python

Integers in Python are wholesome numbers, whereas float values are numbers with decimals.

Python int and Python floatIf you are working on a number-sensitive program like trading software, you need precision in decimal points, too. Therefore, if our calculations might result in fractional values, we will need a floating value that provides greater accuracy and flexibility.

The easiest and most efficient way to convert an integer to a float (double) value is using the “float()” constructor. It accepts an integer value and returns the float.

Converting Python integer to float

num_int = 21

print(num_int)
# Output: 21

print(type(num_int))
# Output: <class 'int'>

# Conversion
num_float = float(num_int)

print(num_float)
# Output: 21.0

print(type(num_float))
# Output: <class 'float'>

We verified the conversion using the type() function that returns the type of variables.

Implicit conversion in arithmetic operations

If you are performing an arithmetic operation and your one operand is an integer and another is a float, Python implicitly promotes the integer to a float in the output.

Implicit conversion in arithmetic operations

num_int = 21
num_float = 19.21

# Arithmetic operation of summation
sum = num_int + num_float

print(sum)
# Output: 40.21

print(type(sum))
# Output: <class 'float'>

This float conversion happens automatically and implicitly.

Division and float promotion

If you perform a division operation using “/“, it always returns the float value, even when dividing integers.

Division and float promotion

num_int = 21
another_int = 7

# Arithmetic operation of division
division = num_int / another_int

print(division)
# Output: 3.0

print(type(division))
# Output: <class 'float'>

The above code shows that even if 21 and 7 are pure integers, the output is in float format because of the “/” operator.

Converting a list of integers to floats

Real-life application mostly has a collection of integers in the format of a list. In that scenario, you can use list comprehension to convert that list of integers to a list of floats with the help of the float() function.

Converting a list of integers to floats

int_list = [21, 19, 18, 20]

print(int_list)
# Output: [21, 19, 18, 20]

# Using list comprehension to convert the list of integers to a list of floats
float_list = [float(x) for x in int_list]

print(float_list)
# Output: [21.0, 19.0, 18.0, 20.0]

Converting booleans

Booleans are subclass of integers which means 1 is equal to True and 0 is equal to False.

  1. If you convert True to float, it returns 1.0.
  2. If you convert False to float value, it returns 0.0.
print(float(True))
# Output: 1.0

print(float(False))
# Output: 0.0

Converting an array of integers

If you are computing numerical values, you might use the Numpy library, which provides an array type.

If your input array is filled with integers, you can use the .astype() function to convert it into an array of floats.

Converting an array of integers to array of floats

import numpy as np

int_array = np.array([11, 21, 31])

print(int_array)
# Output: [11 21 31]

# Conversion using .astype() method
float_array = int_array.astype('float64')

print(float_array)
# Output: [11. 21. 31.]

Custom Objects with __float__()

If your requirement is to customize the float conversion, you can define the built-in private method “__float__()” method.

class CustomNumber:
    def __init__(self, value):
        self.value = value

    def __float__(self):
        return float(self.value)


obj = CustomNumber(21)

float_obj = float(obj)

print(float_obj)
# Output: 21.0

print(type(float_obj))
# Output: <class 'float'>

Precision loss with large integers

In Python, floats have limited precision (64-bit double-precision), so converting very large integers may cause rounding errors.

large_int = 2**53

float_num = float(large_int)

print(large_int == float_num)
# Output: True (no loss)

larger_int = 2**54 + 1

floater_num = float(larger_int)

print(floater_num == larger_int)
# Output: False (precision loss)

That’s all!

Post Views: 25
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Convert Numpy Array to Python Tuple
How to Convert String to Int in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend