Python 中round() 函数有什么用

2023-06-05,

这篇文章给大家介绍Python 中round() 函数有什么用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

描述

round() 方法返回浮点数x的四舍五入值。


语法

以下是 round() 方法的语法:

round( x [, n]  )

参数

  • x -- 数值表达式。

  • n -- 数值表达式,表示从小数点位数。


返回值

返回浮点数x的四舍五入值。


实例

以下展示了使用 round() 方法的实例:

实例

#!/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 中round() 函数有什么用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

《Python 中round() 函数有什么用.doc》

下载本文的Word格式文档,以方便收藏与打印。