How to Convert float to int in Python

We have used different numeric data types in Python, in this tutorial we will learn how we can convert a float value to an integer value.

Let's have a look at the approaches to implement the same-

  1. Using trunc()
  2. Using floor()
  3. Using ceil()
  4. Using int()

So, let's get started with the first one-

Using trunc()

The following program shows how we can use trunc() to convert a float value to an integer in Python.

Output:

The result of a + b + c is 42.33
The converted value of a is: 20
The converted value of b is: 12
The converted value of c is: 9
The converted value of sum is: 42

Explanation-

It's time to understand what happened in the above program-

  1. Since we have to use trunc(), we have imported math in the first step.
  2. After this, we have initialized three float values and then calculated their sum.
  3. Finally, we have passed the variables a, b, c, and res_sum in trunc() to obtain the integer values.
  4. On executing the program, we obtain the desired output.

In the next program, we will make use of floor().

Using floor()

First, let's understand what happens when we pass a float value in floor()?

When a float number is passed in floor(), it rounds off the number down to the nearest integer.

Consider the program given below,

Output:

The result of a + b + c is 42.33
The converted value of a is: 20
The converted value of b is: 12
The converted value of c is: 9
The converted value of sum is: 42

Explanation-

Let's have a glance at the explanation of this program.

  1. Since we have to use floor(), we have imported math in the first step.
  2. After this, we have initialized three float values and then calculated their sum.
  3. Finally, we have passed the variables a, b, c, and res_sum in the floor() to obtain the integer values.
  4. On executing the program, we obtain the desired output.

Now, we shall see how ceil() can be used to do the same.

Using ceil()

First, let's understand what happens when we pass a float value in ceil()?

When a float number is passed in ceil(), it rounds off the number up to the nearest integer.

Output:

The result of a + b + c is 42.33
The converted value of a is: 21
The converted value of b is: 13
The converted value of c is: 10
The converted value of sum is: 43

Explanation-

Let's understand what we have done in this program.

  1. Since we have to use ceil(), we have imported math in the first step.
  2. After this, we have initialized three float values and then calculated their sum.
  3. Finally, we have passed the variables a, b, c, and res_sum in ceil() to obtain the integer values.
  4. On executing the program, we obtain the desired output.

Finally, in the last program, we will use the most basic approach of converting a float value to an integer which is using int().

Using int()

The program given below illustrates how it can be used in a program.

Output:

The result of a + b + c is 42.33
The converted value of a is: 20
The converted value of b is: 12
The converted value of c is: 9
The converted value of sum is: 42

Explanation-

  1. We have initialized three float values and then calculated their sum.
  2. Finally, we have passed the variables a, b, c, and res_sum in int() to obtain the integer values.
  3. On executing the program, we obtain the desired output.

Conclusion

In this tutorial, we learned the interesting methods of converting a float to an integer in Python.






Latest Courses