Format string, integer with leading zeros

后端 未结 2 1991

I Use this code:

NSString* strName = [NSString stringWithFormat:@\"img_00%d.jpg\", pp];

and works fine, but if pp took the value of 10 for

相关标签:
2条回答
  • 2020-11-29 00:43

    For posterity: this works with decimal numbers.

    NSString *nmbrStr = @"0033620340000" ;
    NSDecimalNumber *theNum = [[NSDecimalNumber decimalNumberWithString:nmbrStr]decimalNumberByAdding: [NSDecimalNumber one]] ;     
    NSString *fmtStr = [NSString stringWithFormat:@"%012.0F",[theNum doubleValue]] ;
    

    Though this information is hard to find, it is actually documented here in the second paragraph under Formatting Basics. Look for the % character.

    https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html

    0 讨论(0)
  • 2020-11-29 00:53

    Use the format string "img_%03d.jpg" to get decimal numbers with three digits and leading zeros.

    0 讨论(0)
提交回复
热议问题