SuperString SuperString - 5 months ago 15
Python Question

truncate to 3 decimals in python

Self explanatory, I want to get 1324343032.324

As you can see below, the following do not work:

>>1324343032.324325235 * 1000 / 1000
1324343032.3243253
>>int(1324343032.324325235 * 1000) / 1000.0
1324343032.3239999
>>round(int(1324343032.324325235 * 1000) / 1000.0,3)
1324343032.3239999
>>str(1324343032.3239999)
'1324343032.32'

Answer

'%.3f'%(1324343032.324325235)

Use an additional float() around it if you want to preserve it as a float.