Three ways for python to save the number of decimal places of float type

a = 5.026
b = 5.000

The first method: round(num,n)

print(round(a,2))   #5.03,这个得到的是float类型

The second method:

print(float('%.2f' % a))   #5.03,这个得到的是float类型

The third method:

print('{x:.2f}'.format(x=a))   #5.03,这个得的是str

Guess you like

Origin blog.csdn.net/weixin_45928096/article/details/126783541