加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[IOSS]Swift数据类型转换

发布时间:2020-12-14 01:39:00 所属栏目:百科 来源:网络整理
导读:[IOSS]Swift数据类型转换 /* 整型 - 浮点 */ let intLet : Int = 5 let doubleLet : Double = Double(intLet) let floatLet : Float = Float(intLet) print(intLet,doubleLet,floatLet) /* 字符串 - 整型 */ //swift1.x的语法 //let intString: String = "25

[IOSS]Swift数据类型转换


/*
        整型 -> 浮点
        */
        let intLet : Int = 5
        let doubleLet : Double = Double(intLet)
        let floatLet : Float = Float(intLet)
        print(intLet,doubleLet,floatLet)
        
        /*
        字符串 -> 整型
        */
        //swift1.x的语法
        //let intString: String = "256"
        //let transformInt: Int? = intString.toInt()
        //swift2.x的语法
        let intString: String = "123456"
        let transformInt: Int? = Int(intString)
        print(transformInt!)
        print(transformInt) //不加" !"时打印后后有默认值 Optional(123456)

        /*
        整型 -> 字符串
        */
        let intFive : Int = 5
        let strFive : String = String(intFive)
        print(strFive)
        
        /*
        浮点 -> 字符串
        */
        let double : Double = 20.12
        let stringDouble = NSString(format: "%f",double)
        let stringDouble_ : String = String( double )
        print(stringDouble,stringDouble_)
        /*
        字符串 -> 浮点
        */
        let strDouble : String = "20.12"
        let doubleStr : Double = (strDouble as NSString).doubleValue
        let doubleStr_ : Double = NSString(string: strDouble).doubleValue
        print(doubleStr,doubleStr_)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读