Skip to content
Python

Python Numbers: int, Float vs Decimal and Rounding

Ihsan Ullah · Updated · 8 min read
Python course Lesson 6 cover: Python numbers, int, float vs Decimal and rounding

Python course · Module 2: Values · Lesson 6 of 63

Quick answer: Python has int for whole numbers and float for numbers with a decimal point. A float is stored in binary, so 0.1 + 0.2 gives 0.30000000000000004, not 0.3. For money and other values that must be exact in base 10, use Decimal from the decimal module and create it from a string: Decimal("0.1") + Decimal("0.2") gives exactly 0.3. Use float for measurements and science, and Decimal for money.

In this lesson you will learn:

  • the difference between int and float, and how big an int can be
  • the arithmetic operators, including /, //, % and **
  • why floats are not exact and how to compare them safely
  • how round() really works, including the “round half to even” rule
  • how to use Decimal for money, and the mistakes to avoid with it

Before you start: finish Lesson 5, Python Variables: Naming Rules and Examples. All examples were run as scripts with Python 3.14.6. Python 3.15.2, the newest release on python.org, gives the same results.

int and float

Our running example in this lesson is a restaurant bill that three friends share.

friends = 3
bill = 2450.75
video_views = 1_250_000
print(friends, type(friends))
print(bill, type(bill))
print(video_views)
3 <class 'int'>
2450.75 <class 'float'>
1250000
  • 3 has no decimal point, so it is an int (integer).
  • 2450.75 has a decimal point, so it is a float (floating-point number).
  • You can put underscores in long numbers to make them easier to read. The language reference says underscores “are ignored for determining the numeric value”, so 1_250_000 is the same as 1250000.

Never use commas inside a number. Python reads a comma as a separator between two values:

price = 1,500
print(price)
print(type(price))
(1, 500)
<class 'tuple'>

price is not one thousand five hundred. It is a tuple, a pair of two numbers, 1 and 500. You will learn tuples in Lesson 16. Use 1500 or 1_500 instead.

Arithmetic operators

print(7 + 2)
print(7 - 2)
print(7 * 2)
print(7 / 2)
print(8 / 2)
print(7 ** 2)
print(3 + 2.0)
9
5
14
3.5
4.0
49
5.0
OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide8 / 24.0
//floor division7 // 23
%remainder (modulo)7 % 21
**power7 ** 249

Two results surprise beginners:

  • 8 / 2 gives 4.0, not 4. The Python tutorial says: “Division (/) always returns a float.”
  • 3 + 2.0 gives 5.0. When you mix an int and a float, the result is a float.

Floor division and modulo

// divides and rounds the result down to a whole number; for positive numbers it simply drops the part after the decimal point. % gives the remainder: what is left over. They are perfect for our bill when we pay in whole rupees.

bill = 1000
friends = 3
print("Each pays:", bill // friends)
print("Left over:", bill % friends)
print("Both at once:", divmod(bill, friends))

minutes = 135
print(minutes // 60, "hours and", minutes % 60, "minutes")
Each pays: 333
Left over: 1
Both at once: (333, 1)
2 hours and 15 minutes

Each friend pays 333, and 1 rupee is left over. divmod() gives both answers at once. The same idea turns 135 minutes into 2 hours and 15 minutes. Also, n % 2 is 0 when n is even.

Negative numbers

With negative numbers, // rounds down (towards minus infinity), not towards zero:

print(7 // 2)
print(-7 // 2)
print(-7 % 2)
3
-4
1

-7 / 2 is -3.5, and the next whole number below it is -4. The standard types page says the result “is always rounded towards minus infinity”. This keeps the rule x == (x // y) * y + (x % y) true, so -7 % 2 is 1.

How big can an int be?

In many languages an integer has a maximum size. In Python it does not: “Integers have unlimited precision” (standard types page).

print(2 ** 100)
print(type(2 ** 100))
print(len(str(2 ** 1000)))
1267650600228229401496703205376
<class 'int'>
302

2 ** 1000 has 302 digits, and Python handles it easily. One rare limit: turning an int with more than 4,300 digits into text (for example with print()) raises a ValueError by default, as the standard types page explains.

Why floats are not exact

Now the most famous surprise in Python:

import math

total = 0.1 + 0.2
print(total)
print(total == 0.3)
print(math.isclose(total, 0.3))
0.30000000000000004
False
True

A float is stored as a binary (base 2) fraction. The floating-point tutorial explains that “the decimal value 0.1 cannot be represented exactly as a base 2 fraction”, just as 1/3 cannot be written exactly as a decimal (0.3333…). So 0.1 is stored as a number very close to 0.1, and the tiny errors show up when you add. The same page says: “this is not a bug in Python, and it is not a bug in your code either.”

The lesson: never compare floats with ==. Use math.isclose(), which checks that two numbers are very close. (import math loads the math module; Module 6 covers imports.)

Rounding with round()

round(number, digits) rounds to a number of digits after the decimal point. Without digits, it returns an int.

print(round(3.14159, 2))
print(round(7.6))
print(round(2.5))
print(round(3.5))
print(round(2.675, 2))
3.14
8
2
4
2.67

Two of these look wrong but are correct:

  • round(2.5) is 2 and round(3.5) is 4. When a number is exactly halfway, Python rounds to the even choice. The built-in functions page says “if two multiples are equally close, rounding is done toward the even choice”. This is called banker’s rounding, and it avoids always pushing totals upwards.
  • round(2.675, 2) is 2.67, not 2.68. The same page says this “is not a bug”: the float stored for 2.675 is slightly less than 2.675.

round() is fine for showing a measurement. For money, use Decimal.

Decimal: exact decimal numbers for money

The decimal module stores numbers in base 10, the way people write them. It says decimal “is preferred in accounting applications”.

from decimal import Decimal

print(Decimal("0.1") + Decimal("0.2"))
print(Decimal("0.1") + Decimal("0.2") == Decimal("0.3"))
print(Decimal(0.1))
0.3
True
0.1000000000000000055511151231257827021181583404541015625

The first two lines are exact. The third line shows a trap: Decimal(0.1) takes the float 0.1, with its binary error, and copies it exactly. Always create a Decimal from a string, like Decimal("0.1").

Splitting the bill with Decimal

Back to our bill of 2,450.75 shared by 3 friends. First, the float way:

bill = 2450.75
share = bill / 3
print(share)
print(round(share, 2))
816.9166666666666
816.92

Now with Decimal. quantize() rounds to a fixed number of places, and ROUND_HALF_UP gives the “5 rounds up” rule people use for money:

from decimal import Decimal, ROUND_HALF_UP

bill = Decimal("2450.75")
friends = 3
share = bill / friends
print("Exact share:", share)

cents = Decimal("0.01")
share = share.quantize(cents, rounding=ROUND_HALF_UP)
print("Each pays:", share)
print("Collected:", share * friends)

print(Decimal("2.675").quantize(cents, rounding=ROUND_HALF_UP))
Exact share: 816.9166666666666666666666667
Each pays: 816.92
Collected: 2450.76
2.68
  • The exact share has 28 significant digits, the default precision of the decimal module.
  • Each friend pays 816.92, and the three payments add up to exactly 2450.76, one paisa more than the bill. 2,450.75 cannot be split into three equal amounts of whole paisa, so your program must decide who pays one paisa less. Decimal shows that difference exactly.
  • Decimal("2.675") rounds to 2.68, as a person would expect, unlike the float version above.

Decimal needs more typing than float, so use it where exact decimal values matter: prices, bills, balances and tax.

Try it yourself

Exercise 1: Minutes to hours

Convert minutes = 200 into hours and minutes using // and %. Print 3 hours and 20 minutes.

Exercise 2: Shopping cart total

Three items cost 19.99, 5.49 and 3.75. Add them as floats and print the result. Then add them as Decimals and print that result. Which one is right?

Exercise 3: Remainders

For year = 2026, print the last digit of the year and the remainder after dividing by 4. Use only %.

Solutions

Solution 1

minutes = 200
hours = minutes // 60
rest = minutes % 60
print(hours, "hours and", rest, "minutes")
3 hours and 20 minutes

200 // 60 is 3 whole hours, and 200 % 60 is the 20 minutes left over.

Solution 2

from decimal import Decimal

print(19.99 + 5.49 + 3.75)
print(Decimal("19.99") + Decimal("5.49") + Decimal("3.75"))
29.229999999999997
29.23

The float sum carries a tiny binary error; the Decimal sum, made from strings, is exactly 29.23.

Solution 3

year = 2026
print("Last digit:", year % 10)
print("Remainder after dividing by 4:", year % 4)
Last digit: 6
Remainder after dividing by 4: 2

The remainder after dividing by 10 is always the last digit of a positive whole number.

Common mistakes

  • Expecting / to give a whole number. 8 / 2 is 4.0. Use // when you want an int.
  • Comparing floats with ==. 0.1 + 0.2 == 0.3 is False. Use math.isclose(), or Decimal for money.
  • Making a Decimal from a float. Decimal(0.1) keeps the binary error. Write Decimal("0.1").
  • Mixing Decimal and float. Decimal("19.99") + 0.5 fails with TypeError: unsupported operand type(s) for +: 'decimal.Decimal' and 'float'. Make both values Decimals: Decimal("19.99") + Decimal("0.5").
  • Dividing by zero. bill / friends with friends = 0 stops the program with ZeroDivisionError: division by zero. Check the divisor first (you will learn if in Lesson 11).

Frequently asked questions

Why is 0.1 + 0.2 not equal to 0.3 in Python?

Floats are stored as binary fractions, and 0.1 and 0.2 have no exact binary form, so each is stored as a very close approximation. Adding them gives 0.30000000000000004. This is how binary floating point works in almost every language, not a Python bug.

Should I use float or Decimal for money in Python?

Use Decimal, created from strings, and round with quantize(). Floats are fine for measurements such as distance or temperature, where a tiny error does not matter, but money needs exact decimal values.

Why does round(2.5) return 2 in Python?

Python’s round() uses “round half to even”: when a number is exactly halfway, it goes to the nearest even number, so round(2.5) is 2 and round(3.5) is 4. If you need “5 always rounds up”, use Decimal with ROUND_HALF_UP.

Summary

  • int is for whole numbers and has no size limit; float is for numbers with a decimal point.
  • / always returns a float; // gives the floor and % the remainder, and both round down with negative numbers.
  • Floats are binary approximations, so compare them with math.isclose(), not ==.
  • round() rounds halves to the even number, and round(2.675, 2) gives 2.67 because of the float error.
  • For money, use Decimal("...") from a string and quantize() with the rounding rule you need.

Related lessons: Python operators, Python type conversion, Python for data science.

Next lesson

Next: Lesson 7, Python Strings and f-Strings: Quotes, Escapes and Formatting.

Sources

Ihsan Ullah

Written & tested by Ihsan Ullah

Data analyst with 3 years of experience and a Bachelor's degree in Computer Science. I write clear, practical guides on data, SQL, Python and AI.

More about me →

Leave a comment

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