Python2.4.3: format error?
This is not a format error. This is just floating point arithmetic. Look at the values ββthat underlie your format commands:
In [18]: float('0.00355') Out[18]: 0.0035500000000000002 In [19]: float('0.00355')*100 Out[19]: 0.35500000000000004 In [20]: 0.355 Out[20]: 0.35499999999999998 Two expressions create different meanings.
I don't know if it is available in 2.4, but you can use the decimal module to make this work:
>>> import decimal >>> "%.2f" % (decimal.Decimal('0.00355')*100) '0.35' The decimal module treats floats as strings to preserve arbitrary precision.
Since, as in all issues of "inaccuracy" with a floating point, not every real number can be represented in a limited number of bits.
Even if we went for nuts and had 65536-bit floating point formats, the number of numbers between 0 and 1 is still ... well, endlessly :-)
What almost certainly happens is that the first is slightly lower than 0.355 (say 0.35549999999999), and the second is slightly higher (say 0.35550000001).
See here for further reading on this.
A good tool for the game to see how the floating point numbers Harald Schmidt works great online converter . It was so convenient, I actually implemented my own C #, and am also capable of handling both IEEE754 and double precision.
Arithmetic with floating point numbers is often inaccurate.
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems