How to round a floating-point number to an integer in Python

2 Answers

0 votes
x = 9382.4
y = int(x + 0.5)
print(y)

x = 9382.5
y = int(x + 0.5)
print(y)

 
 
'''
run:
 
9382
9383
 
'''

 



answered May 14, 2025 by avibootz
0 votes
from decimal import Decimal, Context, ROUND_HALF_UP

con = Context(rounding=ROUND_HALF_UP)

x = 9382.4
y = round(Decimal(x, con))
print(y)

x = 9382.5
y = round(Decimal(x, con))
print(y)

x = 9382.6
y = round(Decimal(x, con))
print(y)

 
 
'''
run:
 
9382
9382
9383
 
'''

 



answered May 14, 2025 by avibootz

Related questions

...