I'd like to convert an Int
in Swift to a String
with leading zeros. For example consider this code:
for myInt in 1...3 {
print("\(myInt)")
}
Currently the result of it is:
1
2
3
But I want it to be:
01
02
03
Is there a clean way of doing this within the Swift standard libraries?
in Xcode 8.3.2, iOS 10.3 Thats is good to now
Sample1:
Sample2:
The other answers are good if you are dealing only with numbers using the format string, but this is good when you may have strings that need to be padded (although admittedly a little diffent than the question asked, seems similar in spirit). Also, be careful if the string is longer than the pad.
Output
"-----a str"