Skip to content

Module 3 · Variables and Data Types — topic 6 of 15

Floating-point numbers

The float type for decimal numbers: writing floats, scientific notation, why 0.1 + 0.2 isn’t exactly 0.3, rounding correctly, and when to use Decimal instead.

Beginner 14 min read·Lesson 34 of 306 in Python Programming

Learning objectives

  • Write float literals including scientific notation
  • Explain floating-point rounding error
  • Round and compare floats safely

Definition

The float type for decimal numbers: writing floats, scientific notation, why 0.1 + 0.2 isn’t exactly 0.3, rounding correctly, and when to use Decimal instead.

Introduction

Floats represent numbers with a fractional part, such as 3.14, -0.5 or 2.0. They can also represent very large and very small numbers using scientific notation, such as 6.02e23.

Why it matters

Measurements, percentages, averages and scientific values are floats. Floats are fast and precise enough for most purposes, but they store numbers in binary, which causes tiny rounding differences you must handle when comparing or displaying values.

Detailed explanation

Any number with a decimal point is a float: 2.0 is a float even though it is whole. Dividing with / always produces a float.

Scientific notation: 1.5e3 is 1500.0 and 2e-4 is 0.0002. The e means “times ten to the power of”.

Floats are stored in binary with about 15–17 significant digits. Many decimal fractions, such as 0.1, can’t be represented exactly in binary, so small errors appear: 0.1 + 0.2 gives 0.30000000000000004.

Never compare floats with == after calculations. Use math.isclose(a, b) or compare the difference with a small tolerance.

round(x, 2) rounds for display or storage. Python rounds exact halves to the nearest even digit (banker’s rounding), so round(2.5) is 2 and round(3.5) is 4.

For money and other values that must be exact in decimal, use integers (paise) or the decimal module’s Decimal type.

Special values exist: float("inf") for infinity and float("nan") for “not a number”.

Syntax

Python
price = 49.99
avogadro = 6.022e23
ratio = 3 / 4
rounded = round(value, 2)
  • A decimal point makes a float.
  • e writes scientific notation.
  • / always returns a float.
  • round(value, ndigits) rounds to that many decimal places.

Code examples

Floats and scientific notation

Python
print(3 / 4)
print(1.5e3, 2e-4)
print(type(2.0).__name__)
Output
0.75
1500.0 0.0002
float

The classic rounding surprise

Python
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
Output
0.30000000000000004
False

More examples

Comparing and handling money safely

Python
import math
from decimal import Decimal
total = 0.1 + 0.2
print(math.isclose(total, 0.3))
print(round(total, 2))
print(round(2.5), round(3.5))
price = Decimal("19.99")
print(price * 3)
print(f"{19.99 * 3:.2f}")
Output
True
0.3
2 4
59.97
59.97

Step-by-step explanation

  • math.isclose checks whether two floats are equal within a tiny tolerance.
  • round(total, 2) gives 0.3 for display.
  • round(2.5) and round(3.5) show banker’s rounding to the nearest even number.
  • Decimal("19.99") stores the decimal value exactly, so multiplying by 3 gives exactly 59.97.
  • Formatting with :.2f is the usual way to display a float with two decimals.

Expected output

Running “Comparing and handling money safely” prints exactly:

TextOutput
True
0.3
2 4
59.97
59.97

Practical applications

  • Scientific and engineering software relies on floats for measurements and simulations.
  • Financial systems use Decimal or integer paise so totals always match to the last digit.
  • Machine-learning libraries use large arrays of floats for model weights.

Best practices

  • Format floats for display with f"{x:.2f}" rather than relying on raw output.
  • Use math.isclose() for comparisons.
  • Use Decimal or integers when exact decimal results matter.

Common mistakes

Comparing calculated floats with ==

Avoid

if price * 3 == 59.97:

Prefer

if math.isclose(price * 3, 59.97):

Binary rounding may make the values differ in the last digit.

Using floats for money totals

Avoid

total = 0.1 + 0.2 # rupees

Prefer

total_paise = 10 + 20

Repeated float arithmetic accumulates tiny errors; integers or Decimal stay exact.

Key takeaways

  • Floats store decimal numbers approximately in binary.
  • 0.1 + 0.2 is not exactly 0.3; compare with math.isclose().
  • round() uses banker’s rounding; Decimal gives exact decimal arithmetic.

Try it yourself

  1. 1
    Add 0.1 ten times with a loop and print the total and whether it equals 1.0.
  2. 2
    Compute the average of 72.5, 88 and 91.25 and print it with one decimal place.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic