将NSInteger转换为NSString作为5位数字

tch*_*ike 1 objective-c nsstring nsinteger stringwithformat

我有一个int,我将它转换为NSString,如下所示:

NSString *myStr = [NSString stringWithFormat:@"%d",myInt];
Run Code Online (Sandbox Code Playgroud)

myInt的范围是0到99999.我想要的是将myStr作为5位数字.

例如:00072或09856或00002

谢谢.

小智 6

使用%05d格式说明符以使用零填充:

NSString *myStr = [NSString stringWithFormat:@"%05d", myInt];
Run Code Online (Sandbox Code Playgroud)

维基百科的解释.