I have a changing variable that is a floating point (ex: 2.003
)
And I want to automatically change it to only 2 decimal places.
I have a changing variable that is a floating point (ex: 2.003
)
And I want to automatically change it to only 2 decimal places.
You can use the round function:
print round(2.003, 2) # 2.0
print round(2.013, 2) # 2.01
Otherwise keep the precision and only display the number of decimals you want:
print '%.2f' % 2.003 # 2.00
print '%.2f' % 2.013 # 2.01