Convert int to float in Python

To convert an integer to a floating-point number in Python, use float(value). Python can also convert an int to a float implicitly during mixed numeric arithmetic, but explicit conversion is clearer when you want the result type to be obvious.

The official float() documentation says it returns a floating-point number built from a number or a string. For numeric background, Python’s numeric types documentation covers int, float, and mixed arithmetic rules.

Convert int to float with float()

The most direct method is the built-in float() constructor:

number = 7
result = float(number)

print(result)
print(type(result))

Output:

7.0
<class 'float'>

The original variable is still an integer unless you assign the converted value back to it:

number = 7
number = float(number)

print(number)

Implicit int to float conversion

Python converts an integer to a float automatically when an arithmetic expression mixes int and float. This is called mixed arithmetic. The numeric types documentation explains that if either operand is a floating-point number, the other operand is converted to floating point for the operation.

a = 5
b = 2.5

result = a + b

print(result)
print(type(result))

Output:

7.5
<class 'float'>

Implicit conversion is normal in calculations, but do not add 0.0 just to force a conversion. Use float(number) instead because it communicates your intent.

Division already returns a float

Regular division with / returns a float, even when both operands are integers:

print(8 / 2)
print(type(8 / 2))

Output:

4.0
<class 'float'>

Floor division with // behaves differently. If you are comparing division operators, read Python Pool’s guide to integer division in Python and the tutorial on Python divmod().

Convert a string integer to float

float() can also convert a numeric string:

value = "42"
result = float(value)

print(result)

Output:

42.0

The string must contain a valid numeric value. This fails because the text contains non-numeric words:

float("42 apples")

Output:

ValueError: could not convert string to float: '42 apples'

Precision note

Python integers have unlimited precision, but floating-point numbers use finite precision. That means a very large integer can lose exactness after conversion:

number = 10**20 + 1
result = float(number)

print(result)

Output:

1e+20

For formatting rounded decimal output, see Python round to two decimals and Python round().

When Not to Convert int to float

Use float for measurements, ratios, averages, plotting values, and scientific calculations where binary floating-point precision is acceptable. Do not convert to float just to display a decimal point, and avoid it for money or exact decimal rules.

For exact decimal arithmetic, Python provides the decimal module. The standard library documentation notes that decimal arithmetic is useful when exact decimal representation and controlled rounding matter, such as accounting-style calculations.

from decimal import Decimal

amount = Decimal(10)
price = Decimal("2.50")

print(amount * price)

Output:

25.00

Quick Summary

  • Use float(number) for explicit int-to-float conversion.
  • Mixed arithmetic with a float produces a float result.
  • / returns a float; // is floor division.
  • Use numeric strings only when converting strings with float().

Related Python Pool references: Python data types, Python int to binary, and Python float to string.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted