如何表示多位数的整数?例如,将1表示为01

时间:2021-04-25 19:59:20

How could I NSLog/Output to a label an integer such as 7 as 07 rather than just 7?

我怎么能NSLog /输出到一个整数,如7作为07而不是7?

I've tried %ii and %2i without success, and searched all over Google for a solution!

我已经尝试了%ii和%2i但没有成功,并在谷歌搜索了一个解决方案!

Surely there's a way to do it!?

当然有办法做到这一点!?

Anyone? Thanks.

3 个解决方案

#1


5  

NSLog/printf format allows %02d.

NSLog / printf格式允许%02d。

#2


2  

Just use

NSLog(@"%02d",07)

It will output: 07

它将输出:07

#3


0  

NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
[numFormatter setPaddingCharacter:@"0"];
[numFormatter setFormatWidth:2];
NSString *paddedString = [numFormatter stringFromNumber:[NSNumber numberWithInteger:integer]];
[numFormatter release];

#1


5  

NSLog/printf format allows %02d.

NSLog / printf格式允许%02d。

#2


2  

Just use

NSLog(@"%02d",07)

It will output: 07

它将输出:07

#3


0  

NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
[numFormatter setPaddingCharacter:@"0"];
[numFormatter setFormatWidth:2];
NSString *paddedString = [numFormatter stringFromNumber:[NSNumber numberWithInteger:integer]];
[numFormatter release];