在python中,有一个内置函数round(),
它像这样舍入一个数字:
round(1900, -3) == 2000
是否有一个内置函数可以向下舍入一个数字,如下所示:
function(1900, -3) == 1000
发布于 2018-08-23 15:44:41
您可以使用楼层分割:
def round_down(x, k=3):
n = 10**k
return x // n * n
res = round_down(1900) # 1000
math.floor
也可以工作,但性能有所下降,请参阅Python integer division operator vs math.floor。
发布于 2018-08-23 13:46:41
也许你可以这样试一下
import math
math.floor(1900 / 100) * 100
发布于 2018-08-23 13:48:10
math.floor([field])
向下舍入到下一个整数
math.ceil([field]/1000)*1000
向下舍入到下一个1000
也许你可以在那之后做一个整型转换。
如果你喜欢指数参数的语法,你可以定义你自己的函数:
import math
def floorTo10ths(number, exp):
return int(math.floor(number/10**exp) * 10**exp)
floorTo10ths(1900, 3)
https://stackoverflow.com/questions/51978926
复制相似问题