Python数字round()方法

Python数字round()函数是Python中的一个内置函数。 它返回x的小数点指定舍入n位数后的值。

语法

round( x [, n]  )

参数

  • x - 这是一个数字表达式。
  • n - 表示从小数点到最后四舍五入的位数。默认值为0

返回值

  • 该方法返回x的小数点舍入为n位数后的值。

示例

以下示例显示了round()方法的用法。

#!/usr/bin/python3

print ("round(70.23456) : ", round(70.23456))
print ("round(56.659,1) : ", round(56.659,1))
print ("round(80.264, 2) : ", round(80.264, 2))
print ("round(100.000056, 3) : ", round(100.000056, 3))
print ("round(-100.000056, 3) : ", round(-100.000056, 3))

当运行上述程序时,它会产生以下结果 -

round(70.23456) :  70
round(56.659,1) :  56.7
round(80.264, 2) :  80.26
round(100.000056, 3) :  100.0
round(-100.000056, 3) :  -100.0

上一篇: Python数字 下一篇: Python字符串