Ron said:
In addition to what you find in the above link, the round function can
be used.
p = 1 #digits of precision after decimal
a, b, c = 1.0, 3.05, 4.6
print round(a+b+c,p)
-> 8.6
You also have the option to use the print statements '%' operator to
format the output.
a, b, c = 1.0, 3.05, 4.6
print "%.1f"%(a+b+c)
-> 8.6
_Ron
Just to clarify this, It was pointed out to me that round() doesn't
resolve the problem of floating points, and I agree. (Thanks Fredrik)
8.5999999999999996
The print statement displays it as 8.6.
The only way to avoid the problem completely is by using an alternative
numeric system such as decimal.
What rounding and print formatting do is give you some control within
the specifications of your requirements. Print for display purposes,
and round to minimize errors to within the needed precision.
.... x += 1.6
....1600001.6000213262
Eventually this could be significant.
.... x = round(x+1.6,1)
....3200001.6000000001
Here the error has been kept to a minimum. In most cases, it isn't a
problem, but it is something to be aware of. It does matter in banking
and I beleive there are standard ways of dealing with it.
Cheers,
_Ron