Convert float number to int in Python

Converting a float number to an integer in Python can be done using several different methods, depending on how you want to handle the decimal portion. Here are some common methods:

Using int()

The int() function truncates the decimal portion of the float, effectively rounding towards zero.

float_num = 123.456
int_num = int(float_num)
print(int_num)
 Output: 12

Using round()

The round() function rounds the float to the nearest integer. You can specify the number of decimal places to round to (default is 0).

float_num = 123.456
int_num = round(float_num)
print(int_num)
 Output: 123
float_num = 123.789
int_num = round(float_num)
print(int_num)
 Output: 124

Using math.floor()

The math.floor() function rounds the float down to the nearest integer.

import math

float_num = 123.456

int_num = math.floor(float_num)
print(int_num)
 Output: 123

Leave a Comment

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

Scroll to Top