Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSLabel: output of leading zeros?

How can I write (using leading zeros)

(@"%d",9);

to be 00009 using a method that may exist in NS?

thanks

like image 652
Kristen Martinson Avatar asked Jan 25 '26 07:01

Kristen Martinson


1 Answers

@LaC has a correct answer. But here's an interesting question: What if you don't know how many 0s you want?

Never fear, you can do this!

int numberOfZeros = 5;
NSLog(@"%0*d", numberOfZeroes, 9);

The "*" tells it to look for an extra int specifying the padding length. Neat, huh?

like image 173
Dave DeLong Avatar answered Jan 26 '26 23:01

Dave DeLong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!