我执行了round(395,-2),它将400作为output.How返回,这件事起作用了。?
发布于 2018-05-28 18:46:23
这是故意的。负值将为10. -1轮至最接近的10轮,-2至最接近的100,等等,参见https://docs.python.org/2/library/functions.html#round。
发布于 2018-05-28 18:45:58
在您提供的示例中,round(395, -2)的意思是第395圈到最近的100。
函数round将所需的精度作为第二个参数。负精度意味着要在小数点之前删除有意义的数字。
round(123.456, 3) # 123.456
round(123.456, 2) # 123.45
round(123.456, 1) # 123.4
round(123.456, 0) # 123.0
round(123.456, -1) # 120.0
round(123.456, -2) # 100.0
round(123.456, -3) # 0.0发布于 2018-05-28 18:52:02
round(number[, ndigits]) -> floating point number用十进制数将一个数字舍入到给定的精度(默认为0位)。这总是返回一个浮点数。精度可能是负值。当它的负值达到10的功率时,这里是它的-2,它会转到100。所以395会转到最近的100,也就是400。
不同的方法
使用内置函数循环():
round(1.2345,2)
1.23或者内置函数格式():
format(1.2345, '.2f')
'1.23'或新样式字符串格式:
"{:.2f}".format(1.2345)
'1.23或旧样式字符串格式:
"%.2f" % (1.679)
'1.68'https://stackoverflow.com/questions/50572004
复制相似问题