How to convert Int to String in Swift? Detailed guide
Hello everyone! I'm new to Swift and ran into a problem when converting a number to a string. I'm writing a simple iOS app — a tip calculator. I need to display the final amount in a UILabel, but the compiler complains.
Here's my code:
```swift
let totalBill: Double = 1250.50
let tipPercentage: Double = 0.15
let tipAmount = totalBill * tipPercentage
let finalAmount = totalBill + tipAmount
// Trying to output the result
resultLabel.text = finalAmount // Error!
```
Xcode gives an error: "Cannot assign value of type 'Double' to type 'String?'". I understand that UILabel expects a string, not a number. But how do I properly convert Int or Double to String?
I tried writing `String(finalAmount)`, but I'm not sure if that's the correct approach. I also saw different methods online: through interpola
No answers yet.
Log in to leave an answer