I have the following code:
print ("Cost: %f per %s" % (mrate[choice], munit[choice]))
Output:
Cost: 25.770000 per 10gm tube
How can I get it to round to two decimals while printing, so that I get 25.77 in the output?
I have the following code:
print ("Cost: %f per %s" % (mrate[choice], munit[choice]))
Output:
Cost: 25.770000 per 10gm tube
How can I get it to round to two decimals while printing, so that I get 25.77 in the output?
If you use %.2f
you will get 2 decimal places for you float like:
value = 25.7700000001
print("%f %.2f" % (value, value))
25.770000 25.77
use round function to restrict decimal places.
print ("Cost: %.2f per %s" % (round(mrate[choice],2), munit[choice]))
Or replace "%f" with "%.2f"
You need to use format specifications in your print
function like this:
value = 25.7700000001
print("%.2f" % (value))