Limiting floats to two decimal points

To limit a float to two decimal points, you can use the round() function. This function will round a number to a specified number of decimal places.

Here is an example:

x = 3.14159265
y = round(x, 2)
print(y)

This will print 3.14, as y has been rounded to two decimal places.

Watch a course Python - The Practical Guide

You can also use the format() function to achieve the same result. This function returns a string representation of a number. You can use the :.2f format specifier to specify that you want a float with two decimal places.

Here is an example:

x = 3.14159265
y = format(x, '.2f')
print(y)

This will also print 3.14.

If you want to actually change the value of the float itself, rather than just printing it, you can use either of these approaches in an assignment statement. For example:

x = 3.14159265
x = round(x, 2)
print(x)

or

x = 3.14159265
x = format(x, '.2f')
print(x)

This will change the value of x to 3.14.