round to two decimal places python 1

round to two decimal places python

>>> x = 13.949999999999999999
>>> x
13.95
>>> g = float("{0:.2f}".format(x))
>>> g
13.95
>>> x == g
True
>>> h = round(x, 2)
>>> h
13.95
>>> x == h
True

Here is what the above code is Doing:
1. We create a float with a value of 13.949999999999999999.
2. We print the value of the float.
3. We create a new float with a value of 13.95.
4. We print the value of the new float.
5. We compare the two floats and print the result.
6. We create a new float with a value of 13.95.
7. We print the value of the new float.
8. We compare the two floats and print the result.

As you can see, the two floats are equal.

Similar Posts