3

I am using some old codes written in Python 3 in my Google-App-Engine project which is using Python 2.7. The different round() algorithms in Python 3 and Python 2 gives me a headache. Is there any convenient way to implement a Python 3's round() method in Python 2.7?

A further question: Python 2 and Python 3 handle the integer operations quite differently. For example the following statements have different outputs in Python 2 and 3:

2/4   # 0 in Python 2, 0.5 in Python 3
round(3/2)  
math.ceil(0.5)   # 1.0 in Python 2, 1 in Python 3

Any easy way to convert codes from Python 3 to Python 2 while keeping the behavior to be exactly the same?

Thanks!

Tao Chen
  • 97
  • 1
  • 7

1 Answers1

4

Banker's rounding is implemented in future. Float division can be made the default with a __future__ import.

from __future__ import division
from future.modified_builtins import round
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358