Output a Number to 2 Decimal Points

I should remember how to do this.

So you have 3.141592653 in a variable and you want to output up to the 2 decimal points. Here is how you can do it.

a = 3.141592653
print("{:.2f}".format(a))

The code above returns string. If you want to convert it to an actual float value, you can surround it with float.

Here is a Python3 way. This is cleaner.

print(f"{a:.2f}")

Author: admin

A software engineer in greater Seattle area

Leave a Reply

Your email address will not be published. Required fields are marked *