Chris
Chris

Reputation: 1037

adding zeros in objective-c string formats

Quick question: I am trying to fill in empty spaces with a specific number of zeroes in an NSString stringWithFormat formatting string.
For example, I want:

@"The number is %d", 5   // I want this to output 'the number is 05'
@"the number is %d", 10  // I want this to output 'the number is 10'

I know how to do this in Java, but I cant seem to find the same function in objective-c.

Any help would be great.

Upvotes: 2

Views: 1395

Answers (2)

BoltClock
BoltClock

Reputation: 723668

If in Java you use System.out.printf(), then it's the same format syntax in Objective-C (and C, for that matter):

NSLog(@"The number is %02d", 5);
NSLog(@"The number is %02d", 10);

Upvotes: 15

Elalfer
Elalfer

Reputation: 5338

In C you can use something like:

char res[255];
sprintf(res, "the number is %02d", 5);

Upvotes: 0

Related Questions