Reputation: 1628
I am trying to find a solution to add zeros in the beginning of the number as per the total input provided.
Example: Number = 100 Total Number of digits = 3
The kind of format i will like to have is 001,002,003 and so on.
Thanks
I found out the solution for the same. Posting it below:
while (totalNumCopy) {
totalNumCopy = totalNumCopy/10;
noOfDigits++;
}
NSMutableString *thumbName = nil;
if(noOfDigits > 0)
{
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setFormatWidth:noOfDigits];
[formatter setPaddingCharacter:@"0"];
[formatter setNumberStyle: NSNumberFormatterPadBeforePrefix];
NSString *stringNumber = [formatter stringFromNumber:[NSNumber numberWithInt:i+1]];
thumbName = [NSString stringWithFormat:@"PageThumb%@.png",stringNumber];
[formatter release];
}
Upvotes: 0
Views: 761
Reputation: 24041
the basic string formatter is like this:
NSLog(@"%03d, %03d, %03d", 1, 2, 3);
the result would be:
001, 002, 003
maybe it helps on you.
Upvotes: 4
Reputation: 5268
NSString *myNumber = [NSString stringWithFormat:@"%03d", number];
i think the above code will help you
Upvotes: 2