zoukankan      html  css  js  c++  java
  • python数据类型转换

     一、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

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

    aa = float("124")     #Correct
    print "aa = ", aa     #result = 124.0 
    bb = float("123.45")  #Correct
    print "bb = ", bb     #result = 123.45
    cc = float(-123.6)    #Correct
    print "cc = ",cc      #result = -123.6
    dd = float("-123.34") #Correct
    print "dd = ",dd      #result = -123.34
    ee = float('123v')    #Error,Can't Convert to float
    print ee 

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

    aa = str(123.4)     #Correct
    print aa            #result = '123.4'
    bb = str(-124.a)    #SyntaxError: invalid syntax
    print bb
    cc = str("-123.45") #correct
    print cc            #result = '-123.45'
    dd = str('ddd')     #correct
    print dd            #result = ddd
    ee = str(-124.3)    #correct
    print ee            #result = -124.3
  • 相关阅读:
    oracle常用数据类型
    oracle修改登录认证方式
    oracle设定用户密码使用时间
    oracle口令管理之允许某个用户最多尝试三次登录
    oracle授权另外一个用户访问自己创建的数据对象
    D. Frequent values
    C. RMQ with Shifts
    B. Balanced Lineup
    A. Test for Job
    NYOJ 16 矩形嵌套
  • 原文地址:https://www.cnblogs.com/anningwang/p/7612411.html
Copyright © 2011-2022 走看看