More advanced String interpolation?

Hi,


I'm new to Swift from an Objective-C background.


I'm confused about string interpolation. All of the examples I've ever seen, as well as all of the instances in existing code that I have read, look like this:

"I have \(dollars) dollars"


In Objective-C, as well as c++ and other languages, we use format specifiers to describe how we want variables to appear inside a string. For numbers, I can specify things like number of decimal places, zero padding, or the desired numerical base. How is this done in Swift?


Thanks,
Frank

Replies

No simple way. You may need to embed an expression which creates a formatted String like:

"I have \(String(format: "%4.2f", dollars)) dollars"


Or, if you do not insist on using String interpolation, this may be preferred:

String(format: "I have %4.2f dollars", dollars)


If you want to propose a new feature for Swift String interpolation, swift.org would be the better place.