round()函数是python中自带的数学函数,用于数字的四舍五入。
round(x,d)------对x保留d位小数,结果四舍五入
如果d==-1时,对个位上的数字进行四舍五入,个位后面的数字一律取零
如果d==-2时,对十位上的数字进行四舍五入,十位后面的数字一律取零
代码演示如下:
print(round(3.1415926))
print(round(3.9415926))
print(round(3.1415926,4))
print(round(314.1415926,-1))#对个位上的数字进行四舍五入
print(round(314.1415926,-2))#对十位上的数字进行四舍五入
运行结果如下: