3

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?

Dietrich
  • 5,241
  • 3
  • 24
  • 36

1 Answers1

0

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

Community
  • 1
  • 1
Zac S
  • 49
  • 1
  • 9