标准类型操作符和内建函数
复数的属性(内建)
int()、math.floor()和round()函数的比较
int() 函数直接截去小数部分
math.floor(
) 得到最接近原数的整形但小于原数的整形
round() 函数得到最接近原数的整形(4舍5入)
abs(num) 返回num的绝对值
coerce(num1, num2) 将num1和num2转换成同一类型,然后以一个元组的形式返回
divmod(num1, num2) 除法--求余运算的结合,返回一个元组(num1/num2, num1%num2),对浮点型和复数进行一下舍入(复数仅取实部的商)
例子:
for eachNum in (.2,.7,1.2 ,1.7, -.2, -7, -1.2, -1.7):
print "int(%.1f)\t%+.1f" % (eachNum,int(eachNum))
print "floor(%.1f)\t%+.1f" % (eachNum,math.floor(eachNum))
print "round(%.1f)\t%+.1f" % (eachNum,round(eachNum))
print "_" * 20
int(0.2)
+0.0
floor(0.2)
+0.0
round(0.2)
+0.0
____________________
int(0.7)
+0.0
floor(0.7)
+0.0
round(0.7)
+1.0
____________________
int(1.2)
+1.0
floor(1.2)
+1.0
round(1.2)
+1.0
____________________
int(1.7)
+1.0
floor(1.7)
+1.0
round(1.7)
+2.0
____________________
int(-0.2)
+0.0
floor(-0.2)
-1.0
round(-0.2)
-0.0
____________________
int(-7.0)
-7.0
floor(-7.0)
-7.0
round(-7.0)
-7.0
____________________
int(-1.2)
-1.0
floor(-1.2)
-2.0
round(-1.2)
-1.0
____________________
int(-1.7)
-1.0
floor(-1.7)
-2.0
round(-1.7)
-2.0