round()函数的用法

python中round()函数的用法

round(a,b)

a 需要进行四舍五入的数字。
b 指定的位数,按此位数进行四舍五入。

注解
如果 b 大于 0,则四舍五入到指定的小数位。
如果 b 等于 0,则四舍五入到最接近的整数。
如果 b 小于 0,则在小数点左侧进行四舍五入。

代码

x = 65451.34566789145
print(x)
print(round(x,1))
print(round(x,2))
print(round(x,6))
print(round(x,-1))
print(round(x,-2))
print(round(x,-3))
print(round(x,0))

运行结果

65451.34566789145
65451.3
65451.35
65451.345668
65450.0
65500.0
65000.0
65451.0

猜你喜欢

转载自blog.csdn.net/asacmxjc/article/details/111144053