Converting CGFloat to String in Swift
You can use string interpolation:
let x: CGFloat = 0.1let string = "\(x)" // "0.1"
Or technically, you can use the printable nature of CGFloat directly:
let string = x.description
The description
property comes from it implementing the Printable
protocol which is what makes string interpolation possible.
The fast way:
let x = CGFloat(12.345)let s = String(format: "%.3f", Double(x))
The better way, because it takes care on locales:
let x = CGFloat(12.345)let numberFormatter = NSNumberFormatter()numberFormatter.numberStyle = .DecimalStylenumberFormatter.minimumFractionDigits = 3numberFormatter.maximumFractionDigits = 3let s = numberFormatter.stringFromNumber(x)