How can I write a function in Python that rounds a number?

TIM*_*MEX 1 python

How can I write a function in Python that rounds a number, in the usual fashion. Numbers less than .5 round down, and .5 and above rounds up.

3.4 -> 3

11.9 -> 12

5.5 -> 6

Cha*_*tin 7

Use the Python function round(). If you want to do it yourself, add 0.5 and then truncate.

  • @Jochen Ritzel:现在向他展示如何将负数四舍五入为整数。 (2认同)