Avatar ·

How to add leading zeros to an Int number in Swift for string formatting?

Hi everyone! I'm new to Swift and ran into a problem while working with numbers. I need to display time in the format "hours:minutes:seconds", where each component always consists of two digits. For example, 9 hours, 5 minutes, and 3 seconds should look like "09:05:03". I get hours, minutes, and seconds as Int: ```swift let hours: Int = 9 let minutes: Int = 5 let seconds: Int = 3 ``` But when I simply convert them to a string, I get "9:5:3" instead of "09:05:03". In other languages (like Java), I would use `String.format("%02d", number)`. In Swift, I tried doing this: ```swift let timeString = "\(hours):\(minutes):\(seconds)" ``` This gives the wrong result. I also tried this: ```swift let timeString = String(hours) + ":" + String(minutes) + ":" + String(seconds) ```