To find the integral value nearest to some value , use this
function:
x
round(x)
The value is returned as a float. In the
case that the fractional part of is exactly one-half, the
returned value is the integer farther from zero.
Examples:
x
>>> 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