For generating Numpy indexes, I need to round a float
to an int
.
In Python 2.7, rounding a float
gives a float
:
>>> round(2.7)
3.0
In Python 3.5, an int
is returned:
>>> round(2.7)
3
np.round()
always returns a float.
Is int(round(2.7))
the canonical approach to round to an integer in a Python 2/3 compatible way?
int(round(2.7))
works for both versions. A similar question but only for Python 2.7 was asked here: Python 2.7: round number to nearest integer - so I would use int(round(x))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.