Swift Placeholder issue

故事扮演 提交于 2019-12-11 21:07:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!