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

George Johnston
  • 31,652
  • 27
  • 127
  • 172
  • possible duplicate of [Zero-pad digits in string](http://stackoverflow.com/questions/324358/zero-pad-digits-in-string) – Deepak Danduprolu Jun 17 '11 at 19:20
  • 1
    @Deepak -- that's php -- not quite the same. – George Johnston Jun 17 '11 at 19:20
  • oops. I linked to the wrong question but you will do what you do in `C` as seen in the answers. – Deepak Danduprolu Jun 17 '11 at 19:24
  • 1
    possible duplicate of [Format string integer with leading zeros](http://stackoverflow.com/questions/2985170/format-string-integer-with-leading-zeros) and [How to pad the integer with the preceeding 0](http://stackoverflow.com/questions/5563825/how-to-pad-the-integer-withe-preceeding-0) and [How to get 1 digit integer into a 2 digits number in a string](http://stackoverflow.com/questions/6253666/) – jscs Jun 17 '11 at 21:06

7 Answers7

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.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
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.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • @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.) – Daniel Dickison Jun 17 '11 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 '11 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. – Deepak Danduprolu Jun 19 '11 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]);
puzzle
  • 6,071
  • 1
  • 25
  • 30
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.

David Brown
  • 13,336
  • 4
  • 38
  • 55
1

with variable num_digits

NSString* format =[NSString stringWithFormat:@"%%0%zdzd", num_digits];
NSString *theString = [NSString stringWithFormat:format, 42];
dklt
  • 1,703
  • 1
  • 12
  • 12
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.

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
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"

MANISH PATHAK
  • 2,602
  • 4
  • 27
  • 31