Swift开发技巧

Swift Int to String

2015-12-13  本文已影响8713人  lxb0706

最近在项目中用到 swift, 涉及到 Int 转 String 类型,需要保留两位数,所以去研究了一下,做个记录

let intValue1 = 2
let strValue1 = String(intValue1)

1.2 String 转 Int

let strValue2 = "123"
let intValue2 = Int(strValue2)
let intValue3 = 1
let formatter = NSNumberFormatter()
formatter.minimumIntegerDigits = 2
let strValue3 = formatter.stringFromNumber(intValue3)

这种方式得到的 strValue3 是 String? 类型的,需要拆包再使用

2.2 这种更为直观

let intValue4 = 5
let strValue4 = String(format:"%02d",intValue4)

这种方法更为我们所熟悉方式,个人比较喜欢这种方式

上一篇 下一篇

猜你喜欢

热点阅读