Int转换Float或Double:
let intVar : Int = 3 let doubleVar : Double = Double(intVar)
Int转换为String:
let intVar : Int = 3 let strVar : String = String(intVar)
String转换为Int:
let strVar : String = "123" let intVar : Int? = strVar.toInt()
Double转换为String:
let doubleVar : Double = 3.14 let strVar : String = String(doubleVar)
Double保留两位小数,需要对Double进行扩展:
extension Double { func format(f: String) -> String { return NSString(format: "%\(f)f", self) } } let myDouble = 1.234567 println(myDouble.format(".2")
String转换Double:
let strVar : String = "3.14" var string = NSString(string: strVar) string.doubleValue