Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to convert float to int in Python

The Basics: Floats and Integers

Before we dive into converting float to int in Python, let's understand these two data types. In the simplest terms, a float is a number that has a decimal point, while an integer is a number without a decimal point. Here's an analogy: imagine a float as a precise measurement on a ruler, while an integer is a round number like the total number of apples in a basket.

In Python, you might encounter a float like 3.14 and an integer like 3. The difference is clear; the float has a decimal and the integer does not.

How Python Stores Floats and Integers

It's worth mentioning that Python stores floats and integers differently. Think of it like two different storage boxes for your toys. One box is for Lego blocks (integers), and the other one is for action figures (floats). You can't store a Lego block in the box for action figures, and vice versa.

So, why would we want to convert a float to an integer? Well, sometimes you only need the whole number part of a float for calculations. For example, if you're counting the number of apples (which can only be a whole number), you don't need the decimal part.

The Python Way: Converting Float to Int

Python makes it incredibly easy to convert a float to an integer. You can use the built-in int() function to do this. Here's how it looks:

x = 3.14
y = int(x)
print(y)

In this code, x is a float. When we pass x to the int() function, it returns the whole number part of x, and we store it in y. The print(y) statement will output 3.

It's like having a pizza with 3.14 slices (yes, a fraction of a pizza slice). When your friend asks how many slices you have, you might say "I have 3 slices" because you can't really count a fraction of a slice.

Beware of the Floor Effect

It's important to note that the int() function in Python always applies a 'floor' operation when converting a float to an integer. This means it always rounds down to the nearest whole number.

Imagine you're climbing a staircase. If you're on the third step, but not yet reached the fourth, someone watching from afar would still say you're on the third step, not the fourth. That's what Python does when converting floats to integers.

For example:

x = 3.99
y = int(x)
print(y)

Even though x is very close to 4, the int() function will still round it down to 3.

The Round Function: A Different Approach

If you want to round to the nearest whole number instead of always rounding down, you can use the round() function before converting to an integer:

x = 3.99
y = int(round(x))
print(y)

In this code, round(x) rounds the float x to the nearest whole number (4 in this case), and then int() converts that rounded float to an integer.

It's like if you're 3.99 steps up a staircase, rounding would say you're closer to the 4th step than the 3rd, so it rounds you up to the 4th step.

Conclusion: Floating with the Current

As you continue your journey in learning Python, you'll find that data types like floats and integers are the building blocks of your programs. Converting between these types, such as changing a float to an integer, is one of the fundamental skills you'll use regularly.

Remember, converting a float to an integer in Python is like a river smoothing out stones into round pebbles. The rough edges (the decimal points) are removed, leaving you with a whole, round number. But be aware of the floor effect, where Python always rounds down when using int(). If you want to round to the nearest whole number, use round() first.

In the end, remember that every float has the potential to become an integer, just as every caterpillar has the potential to become a butterfly in the world of Python. So, keep experimenting and exploring the diverse landscapes of this wonderful programming language!