String formatting of a Double
There's no way to do this using String
interpolation.
You can find a good discussion about this on this Apple Dev Forum post.
You can use NSNumberFormatter
or NSDateFormatter
to format the output.
Alternatively, to use printf
-like syntax, you can still use NSString
:
var str = NSString(format: "Hello, world %.2f", 42.12312312313)
Unsure if there is a pure Swift-way but you can always use NSStrings +stringWithFormat:
:
let str = String(format: "%.2f", 1.23)