xxxxxxxxxx
formatted_float = "{:.2f}".format(a_float)
xxxxxxxxxx
num = 123.4567
formatted_num = '{0:.2f}'.format(num) # to 2 decimal places
# formatted_num = '123.46'
xxxxxxxxxx
>>> foobar = 3.141592
>>> print(f'My number is {foobar:.2f} - look at the nice rounding!')
My number is 3.14 - look at the nice rounding!
xxxxxxxxxx
# round a value to 2 decimal points
value_to_print = 123.456
print(f'value is {round(value_to_print, 2)}')
# prints: value is 123.46
xxxxxxxxxx
import math
radius = 5
area = math.pi * radius * radius
print("Area = %.2f" % area)