How to Convert Int to Float in Python

A calculation or function may need a floating-point value where you have an integer. Python’s float() function returns a float from an integer, such as 42.0 from 42.

This article shows how to convert an integer with float() and check the result’s type.

TL;DR

Call float() with an integer to get a Python float, such as 42.0 from 42.

  • Use float(value) when the value itself should have type float.
  • Check the result with type() when type matters to later code.
  • Large integers can lose precision or exceed the float range.

What Is a Python Number?

Python’s built-in numeric types determine how values behave in arithmetic.

TypeExampleUse
int42Whole counts and indexes
float42.0Values with fractions or ratios
complex1+2jValues with two numeric components

Integers are useful for counts and indexes, while floats represent measurements or ratios that can have a fractional part. A value’s written appearance can differ from its type: 42 and 42.0 look alike in magnitude, but Python stores one as an int and the other as a float.

Python can widen an integer automatically in mixed arithmetic with a float. Division with the slash operator returns a float in Python 3, while floor division discards the fractional remainder. Once floor division has reduced a ratio to zero, conversion gives 0.0 and cannot restore the fraction, so choose division that keeps the fractional result.

Integers also support binary, octal and hexadecimal notation, though each produces an int value. That notation helps when values come from bit masks or protocol definitions instead of counts. Once Python has parsed the number, float() converts its value rather than preserving its original spelling, so parse text using its intended base first.

For explicit type conversion, float(value) keeps intent clear because its purpose is visible at the call site.

How to Convert an Integer to Float Step by Step

Use float(integer) to create a floating-point value from an integer. In my run, the conversion of 42 printed as 42.0, and type() reported float.

Step 1: Pass the integer to float()

Call the built-in float() function with the integer as its argument, then assign the returned value if you need to reuse it. The original integer remains unchanged because int and float values are immutable.

number = 42
converted = float(number)
print(converted)
print(type(converted).__name__)

The exact output from the run is shown below. The .0 suffix is the display of a float with an integral value, and the next line confirms its type.

42.0
float
-12.5
1000.0
7.0
ValueError: could not convert string to float: '12.3.4'
9007199254740993
9007199254740992
False
OverflowError: int too large to convert to float
-3
0.5
0
0.0
Terminal showing Python float conversions, errors, precision, and division
Python float conversion, malformed input and division outputs.

I kept float(value) rather than multiplying by 1.0 because the function states the intended conversion without adding an arithmetic operation. Python 3 division with / also returns a float, but that only helps when division is the operation you need.

Step 2: Convert a value held in a variable

A function call works the same way when the integer comes from a variable, calculation or input that has already been parsed. Keep the original variable if another part of the program still needs the integer value.

item_count = 7
rate = float(item_count)
print(rate)

That statement prints 7.0. The conversion changes the type of rate, not item_count. A function may require a particular numeric type, so check its documented parameter contract before passing a value.

For a closer look at the constructor and its accepted values, see AskPython’s Python float() method guide. Its string parsing examples apply when the original value is text rather than an int.

Edge Cases When Converting Numbers

Conversion behavior depends on the input’s format and magnitude.

GoalExpressionResult or behavior
Convert int to floatfloat(42)42.0
Convert float to intint(-3.8)-3, truncates toward zero
Round a floatround(3.8)4, nearest integer under Python rounding rules

The malformed string produced ValueError: could not convert string to float: ‘12.3.4’ because it contains two decimal points. I also passed a signed decimal with surrounding spaces and exponent notation, which returned -12.5 and 1000.0. Validate external text before conversion.

The integer 9007199254740993 became 9007199254740992 as a float, and comparing the result with the original returned False. Float spacing grows at large magnitudes, so consecutive integers can share one float value.

Python raised OverflowError: int too large to convert to float for 10**400. This is a range limit rather than invalid syntax. Keep very large values as int when exactness matters, and catch OverflowError if values can exceed the supported float range.

int() truncates toward zero, and my test printed -3 for int(-3.8). Choose round() when the nearest integer is the goal. AskPython’s string-to-int guide covers parsing text, a separate concern.

Conclusion

Use float(value) when an integer needs to become a float, and verify with type() if downstream code depends on the type. Keep exact large integers as int because a float may round them or reject values outside its range.

For the official rules, see Python’s float() documentation and its page on numeric types. The AskPython articles on float() and integer conversion offer related examples.

FAQ

These answers cover common questions about converting between Python numeric types.

How do I convert an int to a float in Python?

Call float(value), such as float(42), which returns 42.0 as a float.

Does converting an int to float change the original variable?

No. float(value) returns a new float value and leaves the original integer unchanged.

Does int() round a float in Python?

No. int() truncates toward zero. Use round() when nearest-integer rounding is intended.

Pankaj Kumar
Pankaj Kumar

I have been working on Python programming for more than 12 years. At AskPython, I share my learning on Python with other fellow developers.

Articles: 261