小白教程
所有教程
关于
Search
172.70.131.169
172.70.131.169
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python3 func number round”的源代码
本页内容
上一节:
Python3_func_number_max
下一节:
Python3_func_reload
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:round()}}[[Category:python3 built in functions|63]] = Python3 round() 函数 = [[python3 number|Python3 数字]] == 描述 == '''round()''' 方法返回浮点数 x 的四舍五入值,准确的说保留值将保留到离上一位更近的一端(四舍六入)。 精度要求高的,不建议使用该函数。 == 语法 == 以下是 round() 方法的语法: <sample title="" desc="" lang="python" hererun="1"> round( x [, n] ) </sample> == 参数 == * x -- 数字表达式。 * n -- 表示从小数点位数,其中 x 需要四舍五入,默认值为 0。 == 返回值 == 返回浮点数x的四舍五入值。 以下展示了使用 round() 方法的实例: <sample title="" desc="" lang="python" hererun="1"> #!/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)) </sample> 以上实例运行后输出结果为: <sample title="" desc="" lang="python" hererun="1"> 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 </sample> 看下官网给的一个例子: <sample title="" desc="" lang="python" hererun="1"> >>> round(2.675, 2) 2.67 </sample> '''按我们的想法返回结果应该是 2.68,可结果却是 2.67,为什么?''' 这跟浮点数的精度有关。我们知道在机器中浮点数不一定能精确表达,因为换算成一串 1 和 0 后可能是无限位数的,机器已经做出了截断处理。那么在机器中保存的2.675这个数字就比实际数字要小那么一点点。这一点点就导致了它离 2.67 要更近一点点,所以保留两位小数时就近似到了 2.67。 [[python3 number|Python3 数字]]
返回至“
Python3 func number round
”。
上一节:
Python3_func_number_max
下一节:
Python3_func_reload