1num = 123.4567
2formatted_num = '{0:.2f}'.format(num) # to 2 decimal places
3# formatted_num = '123.46'
1print(format(432.456, ".2f"))
2
3>> 432.45
4
5print(format(321,".2f"))
6
7>> 321.00
1>>> foobar = 3.141592
2>>> print(f'My number is {foobar:.2f} - look at the nice rounding!')
3
4My number is 3.14 - look at the nice rounding!
5
1>>> from decimal import *
2>>> getcontext().prec = 6
3>>> Decimal(1) / Decimal(7)
4Decimal('0.142857')
5>>> getcontext().prec = 28
6>>> Decimal(1) / Decimal(7)
7Decimal('0.1428571428571428571428571429')
8