How to make int python from Float

Convert floats to ints in Python, with a simple example: int(3.14) yields 3.

Converting Floats to Ints in Python

Python has a variety of ways to convert a float to an int. One of the simplest is to use the built-in int() function. For example, you can convert the float 123.456 to an int with the following code:

float_num = 123.456
int_num = int(float_num)
print(int_num)

This will output 123 as the int.

Another way to convert a float to int is to use the math.floor() function. This will round the float down to the nearest lower integer. For instance, we can convert the float 123.456 to an int with the following code:

import math

float_num = 123.456
int_num = math.floor(float_num)
print(int_num)

This will output 123 as the int.

Finally, you can also use the round() function to convert a float to an int. This will round the float to the nearest integer. For example, we can convert the float 123.456 to an int with the following code:

float_num = 123.456
int_num = round(float_num)
print(int_num)

This will output 123 as the int.

These are just a few of the ways to convert floats to ints in Python. With the right functions, you can easily convert a float to an int in Python.

Answers (0)