問題:
I'd like to convert an Int
in Swift to a String
with leading zeros.我想將 Swift 中的Int
轉換爲帶有前導零的String
。 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?在 Swift 標準庫中是否有一種乾淨的方法來做到這一點?
解決方案:
參考一: https://stackoom.com/question/1jH2D參考二: Leading zeros for Int in Swift