39

I have the following variable:

NSNumber *consumption = [dict objectForKey:@"con"];

Which returns 42. How can I pad this number to 10 digits on the left, leading with zeros. The output should look as,

0000000042

or if it were 420,

0000000420

4

7 Answers 7

100
NSString *paddedStr = [NSString stringWithFormat:@"%010d", 42];

EDIT: It's C style formatting. %nd means the width is at least n. So if the integer is 2 digit long, then you will have length 3 string (when %3d is used). By default the left empty spaces are filled by space. %0nd (0 between % and n) means 0 is used for padding instead of space. Here n is the total length. If the integer is less than n digits then left padding is used.

2
  • 1
    If I were only padding 3 characters would it just be %03d in the format? Jun 17, 2011 at 19:28
  • Please check the edit. Also you can experiment yourself with various lengths and numbers.
    – taskinoor
    Jun 17, 2011 at 19:33
17

The Objective-C way,

NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setPaddingPosition:NSNumberFormatterPadBeforePrefix];
[numberFormatter setPaddingCharacter:@"0"];
[numberFormatter setMinimumIntegerDigits:10];

NSNumber * number = [NSNumber numberWithInt:42];

NSString * theString = [numberFormatter stringFromNumber:number];

NSLog(@"%@", theString);

The C way is faster though.

3
  • @George I don't know about cleaner. NSNumberFormatter is certainly more verbose, but I'd argue it's clearer as far as showing intent. (Granted in this case the sprintf format is not exactly rocket science anyways.) Jun 17, 2011 at 19:35
  • I have never cared whether NSNumberFormatter can do this. Always used C style (may be because I have come from C background). +1 though for the Obj-C style.
    – taskinoor
    Jun 17, 2011 at 19:36
  • @George cleaner surely but this one's clearer as Daniel has said. But for such trivial use case, the C way is the way to go. Jun 19, 2011 at 18:27
8

You can't in the NSNumber itself. If you're creating a string from the number or using NSLog(), simply use the appropriate format, e.g.

NSLog(@"%010d", [consumption intValue]);
3

You can do pretty much any number formatting you would ever want with NSNumberFormatter. In this case I think you would want to use the setFormatWidth: and setPaddingCharacter: functions.

1

with variable num_digits

NSString* format =[NSString stringWithFormat:@"%%0%zdzd", num_digits];
NSString *theString = [NSString stringWithFormat:format, 42];
0

E.g. Fill the rest with zeros, 5 digits:

NSInteger someValue = 10;
[NSString stringWithFormat:@"%05ld", someValue];

Equivalent of .02f for float number when you need only 2 digits after the dot.

So there you have 0 = fill with zeros, 5 = the number of digits and type.

0

Solution for Swift 3

let x = 1078.1243
let numFormatter = NumberFormatter()
numFormatter.minimumFractionDigits = 1 // for float
numFormatter.maximumFractionDigits = 1 // for float
numFormatter.minimumIntegerDigits = 10 // how many digits do want before decimal
numFormatter.paddingPosition = .beforePrefix
numFormatter.paddingCharacter = "0"

let s = numFormatter.string(from: NSNumber(floatLiteral: x))!

OUTPUT

"0000001078.1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.