Swift 中 Int 的前導零 - Leading zeros for Int in Swift

問題:

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章