一、int函数能够
(1)把符合数学格式的数字型字符串转换成整数
(2)把浮点数转换成整数,但是只是简单的取整,而非四舍五入。
举例:
1 aa=int("124")#Correct2 print"aa =", aa#result=1243 bb=int(123.45)#correct4 print"bb =", bb#result=1235 cc=int("-123.45")#Error,Can't Convert to int6 print"cc =",cc7 dd=int("34a")#Error,Can't Convert to int8 print"dd =",dd9 ee=int("12.3")#Error,Can't Convert to int10 printee11
二、float函数将整数和字符串转换成浮点数。
举例:
1 aa=float("124")#Correct2 print"aa =", aa#result = 124.03 bb=float("123.45")#Correct4 print"bb =", bb#result = 123.455 cc=float(-123.6)#Correct6 print"cc =",cc#result = -123.67 dd=float("-123.34")#Correct8 print"dd =",dd#result = -123.349 ee=float('123v')#Error,Can't Convert to float10 printee
三、str函数将数字转换成字符
举例:
1 aa=str(123.4)#Correct2 printaa#result = '123.4'3 bb=str(-124.a)#SyntaxError: invalid syntax4 printbb5 cc=str("-123.45")#correct6 printcc#result = '-123.45'7 dd=str('ddd')#correct8 printdd#result = ddd9 ee=str(-124.3)#correct10 printee#result = -124.3