While working with float precision, I stumbled across a strange fact. Why does python prints only the integer part when formatted with "%.f". I am willing to know the mechanism behind this
>>> a = float(2.12345)
>>> a
2.12345
>>> print "%.2f" % a
2.12
>>> print "%.1f" % a
2.1
>>> print "%f" % a
2.123450
>>> print "%.f" % a
2 #why?
Thanks in advance for the explanation :)
"%.f"is the same as"%.0f"?1.0is the same as1., probably.'.'(dot) followed by the precision. The format specification mini-language documentation similarly requires an argument for the precision. Sinceint()returns 0 it seems like a reasonable default, but undefined behaviour means exactly that.