1.int(x[,base])
(1)把符合数学格式的数字型字符串转换成整数;
(2)把浮点数转换成整数,但是只是简单的取整,而非四舍五入。
aa = int("1234")
print "aa = ", aa
bb = int(123.45)
print "bb = ",bb
cc = int("1234.5")
print "cc = ", cc #双引号内的数不能带有小数点。
输出: aa = 1234
bb = 123
cc会报错
2.long(x[,base])
与int语句的操作方式一样
3.字符串转为浮点数
float(x) 将整数x或字符串x转换成浮点数
aa = float("123")
print "aa = ", aa
bb = float("123.45")
print "bb = ", bb
cc = float("-123.34")
print "cc = ", cc
dd = float("123a") #双引号内不能加字母
print "dd = ", dd
输出:
aa = 123.0
bb = 123.45
cc = -123.34
dd会报错
4.str(x) 数字转换为字符串类型
aa = str("123.4")
print "aa = ", aa
bb = str(-123.5)
print "bb = ",bb
cc = str("-1234.5")
print "cc = ", cc
dd = str("aaa")
print "dd = ", dd
输出:
aa = 123.4
bb = -123.5
cc = -1234.5
dd = aaa
网友评论