开发者

How to pad the integer withe preceeding 0?

I am having an integer val开发者_如何学Pythonue like 6 or 65 . If the value is single digit I want to display it like 06 and if it is 65 then as it is ( 65 only ) . I have searched into the NSNumberFormatter class of iphone but not getting what to do exactly . Can any one help me in this ?

Thanks in advance !!


Use %02d format specifier, e.g.

NSLog(@"%02d", number);

That will output integer number with at least 2 characters length and output will be padded with leading zeroes if required


For using the format specifiers as Vladimir suggested, you can also use the stringWithFormat convenience method on NSString to generate a string from a numeric value in the desired format.


Here is another option, although I think Vladimir's is prob the most clean. :)

NSLog(@"%@%d", number<10?@"0":@"", number);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜