
接下来就将介绍,当我们需要对输出的小数点位数限制的时候,需要怎么办。
-
使用 round:
number就是你想format的数,ndigits就是需要的小数点位数。
1
print(round(number, ndigits))
-
使用 format:
.2f就是你的小数的格式,两个小数点位,number就是你要format的数
1
print ("{0:.2f}".format(number))

接下来就将介绍,当我们需要对输出的小数点位数限制的时候,需要怎么办。
使用 round:
number就是你想format的数,ndigits就是需要的小数点位数。
1 |
print(round(number, ndigits)) |
使用 format:
.2f就是你的小数的格式,两个小数点位,number就是你要format的数
1 |
print ("{0:.2f}".format(number)) |
近期评论