Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to round a float number to the nearest integer in Python [closed]

Tags:

python

The function round(x) in Python yields floating number. Therefore, what is the most appropriate code in Python to round a float to the nearest neighbor?

like image 596
fsaizpoy Avatar asked Sep 18 '25 01:09

fsaizpoy


1 Answers

It seems that round already does what you want, or maybe I did not understand the question.

>>> round(3.2)
3
>>> round(3.8)
4

>>> a = round(3.8)
>>> type(a)
<class 'int'>

EDIT

Python3 round returns an integer, but in Python2.7 round returns a float. For Python2.7 just do:

int(round(x))
like image 84
Simone Zandara Avatar answered Sep 19 '25 16:09

Simone Zandara