Next / Previous / Contents / TCC Help System / NM Tech homepage

20.37. round(): Round to the nearest integral value

To find the integral value nearest to some value x, use this function:

round(x)

The value is returned as a float. In the case that the fractional part of x is exactly one-half, the returned value is the integer farther from zero. Examples:

>>> round ( 4.1 )
4.0
>>> round(4.9)
5.0
>>> round(4.5)
5.0
>>> round(-4.1)
-4.0
>>> round(-4.9)
-5.0
>>> round(-4.5)
-5.0

You can also provide an optional second argument that specifies how many digits to retain after the decimal.

>>> from math import pi
>>> round(pi)
3.0
>>> print round(pi,1)
3.1
>>> print round(pi,2)
3.14
>>> print round(pi, 4)
3.1416
>>> round(pi,30)
3.1415926535897931