String formatting in Swift: how to create a string with value substitution (analog of String(format:))
Hello everyone! I just started learning Swift and am writing my first app for iOS 8. I encountered a problem with string formatting.
I need to display user information on screen: name, age, and account balance. For example: "User: Ivan, age: 25 years, balance: 1234.56 rubles." Previously in Objective-C I used `[NSString stringWithFormat:@"...", ...]`, but in Swift it apparently works differently.
I tried several options, but none give the desired result:
1. Simple interpolation: `"User: \(name), age: \(age) years, balance: \(balance) rubles."` — here I cannot control the number format (for example, display only two decimal places for the balance).
2. I tried using `String(format:)` like this:
```swift
let name = "Ivan"
let age = 25
let balance
No answers yet.
Log in to leave an answer
Blogs