Here is what I have tried:
>>> pi = 3.14159265
>>> format(pi, '.3f') #print 3.142 # 3 precision after the decimal point
'3.142'
>>> format(pi, '.1f') #print 3.1
'3.1'
>>> format(pi, '.10f') #print 3.1415926500, more precision than the original
'3.1415926500'
>>> format(pi, '.5f') #print 3.14159, more precision than the original
'3.14159'
>>> format(pi, '.4f')
'3.1416'
The concern part is this:
>>> format(pi, '.3f') #print 3.142 # 3 precision after the decimal point
'3.142'
>>> format(pi, '.4f')
'3.1416'
Where I was expecting to have 3.1415, I am getting 3.1416. Please suggest me.
The SO is showing these 2 links:
http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate
http://stackoverflow.com/questions/1089018/why-cant-decimal-numbers-be-represented-exactly-in-binary
But these are not the one I am looking for.
3.1416concerning, but3.142fine?decimalmodule has asetcontext(ROUND_DOWN)which may be of use. Obviously you’d have to convert your number to aDecimalobject.