Skip to content Skip to sidebar Skip to footer

Round A Float And Convert It To An Integer In A Python2 And Python3 Compatible Manner

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: >

Solution 1:

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))


Post a Comment for "Round A Float And Convert It To An Integer In A Python2 And Python3 Compatible Manner"