4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

What is float in python?

By 4Geeks

Learn what a float is, convert text and integers with float(), handle decimal surprises like 0.1 + 0.2 and choose Decimal for money.

6 min readLast updated
Float in Python icon

What is a float in Python?

Short answer: a float is Python's type for numbers with a decimal point, like 19.99, 3.5 or -0.25. You get one when you write a number with a dot, when you divide with /, or when you convert text with float("3.5").

python
price = 19.99
print(type(price))      # <class 'float'>
print(7 / 2)            # 3.5
print(float("3.5") + 1) # 4.5

Floats are stored in binary, so some decimals are approximate: 0.1 + 0.2 gives 0.30000000000000004. Round them to show them, and use Decimal for money.

A float is a number with decimals, float() converts text into one and round() hides the tiny binary errors when you show it.

Floats at a glance

What you wantCodeResult
Write a floatprice = 19.9919.99
Check the typetype(19.99) or isinstance(x, float)<class 'float'>, True
Convert text or an intfloat("3.5"), float(7)3.5, 7.0
Divide7 / 23.5 (always a float)
Divide without decimals7 // 23
Drop the decimalsint(3.99)3
Roundround(3.14159, 2)3.14
Show with 2 decimalsf"{3.14159:.2f}""3.14"
Compare safelymath.isclose(0.1 + 0.2, 0.3)True
Exact decimals for moneyDecimal("0.1") + Decimal("0.2")Decimal("0.3")
Scientific notationfloat("1e3")1000.0
Infinity and "not a number"float("inf"), float("nan")inf, nan

How to create a float

Any number with a decimal point is a float, even if the decimals are zero:

python
a = 3.0
b = -0.25
c = 1e3        # 1000.0, scientific notation
 
print(type(a), type(c))   # <class 'float'> <class 'float'>

Mixing an int and a float gives a float: 5 + 2.0 is 7.0. The / operator always returns a float, even when the division is exact: 10 / 2 is 5.0.

Career · AI Engineering

You already work with numbers in Python, now build with AI

From zero to junior AI Engineer: Python, data, AI agents and RAG with mentors and real projects.

Convert to float with float()

float() turns text and integers into floats. It's what you need after input(), which always returns text:

python
height_text = input("Height in meters: ")   # "1.75"
height = float(height_text)
print(height * 100)                      # 175.0
 
print(float("  2.5 "))   # 2.5  spaces are ignored
print(float(7))          # 7.0
print(float(True))       # 1.0

The text must use a dot as the decimal separator. A comma fails:

python
float("3,5")
# ValueError: could not convert string to float: '3,5'
 
float("3,5".replace(",", "."))   # 3.5

To convert a float to an integer, int() drops the decimals without rounding: int(3.99) is 3. To go from the text "3.5" to an integer, convert it to float first: int(float("3.5")).

Why 0.1 + 0.2 is not 0.3

Computers store floats in binary, and many decimals, like 0.1, have no exact binary form. Python keeps the closest value it can, so tiny errors appear:

python
print(0.1 + 0.2)          # 0.30000000000000004
print(0.1 + 0.2 == 0.3)   # False

This happens in every language that uses standard floats, not only Python. There are three ways to handle it:

python
import math
from decimal import Decimal
 
print(round(0.1 + 0.2, 2))                 # 0.3   to show it
print(math.isclose(0.1 + 0.2, 0.3))        # True  to compare
print(Decimal("0.1") + Decimal("0.2"))     # 0.3   exact, for money

Never compare floats with ==. Use math.isclose(), and create Decimal values from text, not from floats.

Format floats for display

Keep the full value for calculations and format it only when you show it:

python
total = 1234567.891
 
print(f"{total:.2f}")    # 1234567.89
print(f"{total:,.2f}")   # 1,234,567.89
print(f"{0.256:.1%}")    # 25.6%

How big and how precise a float is

Python floats are 64-bit numbers. They keep about 15 to 17 significant digits, and the largest value is about 1.8e308:

python
import sys
print(sys.float_info.max)   # 1.7976931348623157e+308
print(sys.float_info.dig)   # 15
print(1e308 * 10)           # inf

Integers in Python have no size limit, but floats do. For very precise calculations, use Decimal or fractions.Fraction.

Check if a value is a float

python
x = 3.0
 
print(isinstance(x, float))   # True
print(x.is_integer())         # True, it has no decimal part
print((3.5).is_integer())     # False

Use AI to work with floats

An assistant like ChatGPT, Claude or GitHub Copilot can explain a strange decimal result and suggest the right type. These three prompts are ready to copy.

1. Explain a result

In Python, [your calculation] returns [the result] and I expected [what you expected].
Explain why and show me how to get the right result.

2. Choose a type

I'm working with [prices / measurements / percentages] in Python.
Should I use float, int or Decimal? Show me an example with my data: [paste a sample].

3. Fix a conversion

This Python line: [paste it] gives this error: [paste the error].
The value comes from [input() / a CSV / an API]. Show me how to convert it safely.

Check the answer with a few values you can calculate by hand.

Learn with a mentor

From floats and decimals to AI applications

In the 4Geeks AI Engineering career program you start with no experience and finish with AI agents and RAG systems in your portfolio.

Common problems with floats in Python

What you seeWhy it happensFix
0.30000000000000004 instead of 0.3Floats are stored in binaryround(x, 2) to show it, Decimal for money
0.1 + 0.2 == 0.3 is FalseThe two values differ by a tiny amountmath.isclose(0.1 + 0.2, 0.3)
ValueError: could not convert string to float: '3,5'The text uses a commafloat(text.replace(",", "."))
ValueError: could not convert string to float: 'abc'The text is not a numberCheck it first, or use try / except ValueError
TypeError: float() argument must be a string or a real number, not 'NoneType'The value is NoneCheck where the value comes from
ValueError: invalid literal for int() with base 10: '3.5'int() can't read decimals from textint(float("3.5"))
ZeroDivisionError: float division by zeroYou divided by 0.0Check the divisor before dividing
Free download

Floats in Python in a 2-page cheat sheet

Creating and converting floats, why 0.1 + 0.2 is not 0.3, comparing and rounding, Decimal for money, display formats, common errors and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

Float in Python cheat sheet

2 pages · PDF · 4Geeks

  • Every float operation in one table
  • How to handle decimal errors and money
  • Common errors and prompts to solve them with AI

Get it free

Your PDF right away

Frequently asked questions about floats in Python

Want to go from your first Python numbers to building AI applications, with real projects and mentors?

See the AI Engineering program