python中使数值四舍五入的方法

小编给大家分享一下python中使数值四舍五入的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

python中四舍五入的方法:

python中可以使用round()方法进行四舍五入计算,round()方法返回浮点数x的四舍五入值。

round() 方法的语法:

round( x [, n]  )

参数

x -- 数值表达式。

n -- 数值表达式。

示例:

#!/usr/bin/python

print "round(80.23456, 2) : ", round(80.23456, 2)
print "round(100.000056, 3) : ", round(100.000056, 3)
print "round(-100.000056, 3) : ", round(-100.000056, 3)

运行结果如下:

round(80.23456, 2) :  80.23

round(100.000056, 3) :  100.0

round(-100.000056, 3) :  -100.0

看完了这篇文章,相信你对python中使数值四舍五入的方法有了一定的了解,想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!