xxxxxxxxxx
for i in range(10):
j=i*0.1
print('%0.2f' %j)
xxxxxxxxxx
float = 2.154327
format_float = "{:.2f}".format(float)
print(format_float)
xxxxxxxxxx
#larryadah
# This is my favorite option
num = 987.654321
rounded_num = round(num, 2) # to 2 decimal places
print(rounded_num) # rounded_num = 987.65
print(type(rouned_num)) # the rounded value remains a 'float'
# However, a second option could be
num = 987.654321
rounded_num = '{0:.2f}'.format(num) # to 2 decimal places
print(rounded_num) # rounded_num = '987.65'
print(type(rouned_num)) # the rounded value becomes a 'string'
#larryadah
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