问题
I have this code
var i = 1
println(i) //result is 1
println(%02i) //is wrong
I want it to output 01 instead of 1
回答1:
Unfortunately, you can't format swift strings like that (as far as I know.) You can try to use an NSString though.
println(NSString(format:"%02i", i))
回答2:
This is it
var i = 1
NSLog("%02d", i)
O/P - 01
回答3:
Your best bet is still going to be NSString formatting:
var i = 3
println("someInt is now \(i)")
// prints "someInt is now 1"
println(NSString(format:"%.2f",i))
// prints "someInt is now 01"
May be this help you.
回答4:
var i = 1
println("0\(i)")
//01
来源:https://stackoverflow.com/questions/24052469/swift-placeholder-issue