Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode iPhone SDK - Keep NSInteger zero at beginning

I want to keep the zero at the beginning of my NSInteger, but when i NSLog it, the zero is removed.

NSInteger myInteger = 05;
NSLog("%d", myInteger);

log: 5

I get 5 instead of 05. How can i keep the 0 at the beginning of the integer?

like image 248
Aleksander Azizi Avatar asked Aug 30 '26 21:08

Aleksander Azizi


1 Answers

NSInteger doesn't do "leading" zeros. You're thinking about a number formatting thing.

If you just want to print out leading zeros via "NSLog", try something like:

NSLog( "%02d", myInteger);

Which instructs NSLog to have two digits and if it doesn't reach two digits, do a leading zero.

Take a look at the printf format specifiers (which NSLog tries to conform to) and you'll see how leading zeros are added there.

like image 137
Michael Dautermann Avatar answered Sep 02 '26 15:09

Michael Dautermann



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!