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?
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With