I am reading a text file with floating point numbers, all with either 1 or 2 decimal points. I am using
[-3.6499999999999999, 9.1699999999999999, 1.0]
'%.1f' % round(n, 1)
[round(num, 2) for num in list]
In a word, you can't.
3.65 cannot be represented exactly as a
float. The number that you're getting is the nearest number to
3.65 that has an exact
The difference between (older?) Python 2 and 3 is purely due to the default formatting.
I am seeing the following both in Python 2.7.3 and 3.3.0:
In : 3.65 Out: 3.65 In : '%.20f' % 3.65 Out: '3.64999999999999991118'
For an exact decimal datatype, see