恨鐵不成金
恨鐵不成金

Reputation: 21

Swift Placeholder issue

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

Upvotes: 0

Views: 1240

Answers (4)

Daxesh Nagar
Daxesh Nagar

Reputation: 1415

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.

Upvotes: 0

Connor
Connor

Reputation: 64674

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))

Upvotes: 2

Chéyo
Chéyo

Reputation: 9367

var i = 1
println("0\(i)")

//01

Upvotes: -1

Kumar KL
Kumar KL

Reputation: 15335

This is it

var i = 1
 NSLog("%02d", i)

O/P - 01

Upvotes: 0

Related Questions