python数据类型转换 python字符串转浮点数 python字符串转float

原文地址为: python数据类型转换 python字符串转浮点数 python字符串转float

 转载 http://www.cnblogs.com/dabiao/archive/2010/03/07/1680096.html

一、int函数能够
     (1)把符合数学格式的数字型字符串转换成整数
     (2)把浮点数转换成整数,但是只是简单的取整,而非四舍五入。


举例:

复制代码
 
  
1 aa = int("124") #Correct 2  print "aa = ", aa #result=124 3  bb = int(123.45) #correct 4  print "bb = ", bb #result=123 5  cc = int("-123.45") #Error,Can't Convert to int 6  print "cc = ",cc 7 dd = int("34a") #Error,Can't Convert to int 8 print "dd = ",dd 9 ee = int("12.3") #Error,Can't Convert to int 10 print ee 11
复制代码

二、float函数将整数和字符串转换成浮点数。

 

举例:

复制代码
 
  
1 aa = float("124") #Correct 2 print "aa = ", aa #result = 124.0 3 bb = float("123.45") #Correct 4 print "bb = ", bb #result = 123.45 5 cc = float(-123.6) #Correct 6 print "cc = ",cc #result = -123.6 7 dd = float("-123.34") #Correct 8 print "dd = ",dd #result = -123.34 9 ee = float('123v') #Error,Can't Convert to float 10 print ee
复制代码

 

三、str函数将数字转换成字符

 

举例:

复制代码
 
  
1 aa = str(123.4) #Correct 2 print aa #result = '123.4' 3 bb = str(-124.a) #SyntaxError: invalid syntax 4 print bb 5 cc = str("-123.45") #correct 6 print cc #result = '-123.45' 7 dd = str('ddd') #correct 8 print dd #result = ddd 9 ee = str(-124.3) #correct 10 print ee #result = -124.3
复制代码

转载请注明本文地址: python数据类型转换 python字符串转浮点数 python字符串转float
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值