python中的数学运算函数(ceil,floor,round)的主要任务是截掉小数以后的位数.总体来说
就是取整用的。只是三者之间有微妙的区别:
floor() :把数字变小
ceil() : 把数字变大。
round() : 四舍五入。
英文不好的笔者,经常把这三者搞混,后来记着了三者的英文名字,就不会忘记了。
floor 是地板,
ceil 是天花板。
round 整整的,圆形的
再用一个简单的栗子加强记忆:
import math
sample = 1.52
print "sample: %f ceil(sample): %f" % (sample,math.ceil(sample))
print "sample: %f floor(sample): %f" % (sample,math.floor(sample))
print "sample: %f round(sample): %f" % (sample,round(sample))
sample = 1.49
print "sample: %f ceil(sample): %f" % (sample,math.ceil(sample))
print "sample: %f floor(sample): %f" % (sample,math.floor(sample))
print "sample: %f round(sample): %f" % (sample,round(sample))
测试结果: