Python中的类似Excel的天花板函数?
我知道math.ceil和numpy.ceil ,但他们都缺乏significance
参数。 例如在Excel中:
=Ceiling(210.63, 0.05)
→ 210.65
另一方面numpy.ceil和math.ceil:
numpy.ceil(210.63)
– > 211.0
math.ceil(210.63)
– > 211.0
所以,我想知道,有没有类似Excel的解决scheme?
我不知道有什么python函数可以这样做,但是你可以很容易地编写一个:
import math def ceil(x, s): return s * math.ceil(float(x)/s)
如果两个参数都是整数,则在Python 2中转换为浮点数是为了避免整数除法。 您也可以from __future__ import division
。 python 3不需要这个。
你可以做的是这个。
ceil = lambda x,y: math.ceil(x*(1.0/y))/(1.0/y)
但这不是万无一失的。