1、下面是一个浮点类型的数字转成String字符串的例子
var f = 123.32342342
var s:String = "\(f)" //123.32342342
2、如果要保留两位小数
var f = 123.32342342
var s = String(format: "%.2f", f) //123.32
3、转成十六进制格式字符串
let i = 255
let s:String = String(format: "%x", i) //ff
4、不足六位前面补0
let i = 255
let s:String = String(format: "%06x", i) //0000ff
网友评论