Python Round to 2 Decimals: 2 Easy Methods

You can use the built-in round function in Python to keep two decimal places, for example:

number = 3.14159
rounded_number = round(number, 2)
print(rounded_number)

This will result in: 3.14

Another method is to use string formatting.

number = 3.14159
formatted_number = "{:.2f}".format(number)
print(formatted_number)

This will also produce: 3.14

No matter which method you use, you can preserve two decimal places in Python.

bannerAds