11

I'm looking for a way to display "1" as "01", so basically everything below 10 should have a leading 0.

What would be the best way to do this? I know I can just use a simple if structure to do this check, but this should be possible with NSNumberformatter right?

woutr_be
  • 9,532
  • 24
  • 79
  • 129
  • See [my answer](http://stackoverflow.com/a/25567534/1966109) that offers up to 3 solutions for a similar question tagged with `swift`. – Imanou Petit Mar 29 '17 at 21:39

2 Answers2

24

If you just want an NSString, you can simply do this:

NSString *myNumber = [NSString stringWithFormat:@"%02d", number];

The %02d is from C. %nd means there must be at least n characters in the string and if there are less, pad it with 0's. Here's an example:

NSString *example = [NSString stringWithFormat:@"%010d", number];

If the number variable only was two digits long, it would be prefixed by eight zeroes. If it was 9 digits long, it would be prefixed by a single zero.

If you want to use NSNumberFormatter, you could do this:

NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPaddingPosition:NSNumberFormatterPadBeforePrefix];
[numberFormatter setPaddingCharacter:@"0"];
[numberFormatter setMinimumIntegerDigits:10];
NSNumber *number = [NSNumber numberWithInt:numberVariableHere];

----UPDATE------ I think this solves your problem:

[_minutes addObject:[NSNumber numberWithInt:i]];
return [NSString stringWithFormat:@"%02d", [[_minutes objectAtIndex:row] intValue]]; 
pasawaya
  • 11,515
  • 7
  • 53
  • 92
  • Thanks, that sure will help. But is it possible that getting the stringValue removes the 0 again? I'm using this in a UIPickerView, but perhaps it's just better to do this in titleForRow – woutr_be Jun 21 '12 at 05:07
  • [_minutes addObject:[NSNumber numberWithInt:i]]; return [NSString stringWithFormat:@"%02@", [_minutes objectAtIndex:row]]; This just seems to return "1", "2" – woutr_be Jun 21 '12 at 05:32
  • A stringValue would not remove the 0 again because a string is a series of characters. In a string, the letter 'a' for example, is the same as a '0'. A string just stores characters, and both letters and numbers are characters. – pasawaya Jun 21 '12 at 12:43
  • @woutr_be See my update. I think I fixed your code that wasn't working. – pasawaya Jun 21 '12 at 17:11
  • sorry for the late reply, the updated code does work indeed, thanks a lot! – woutr_be Jun 25 '12 at 02:23
  • 1
    You don't need to call `setPaddingCharacter`. Just leave it to the `NSNumberFormatter`. :) – Rudolf Adamkovič Jul 02 '14 at 12:41
8

FIXED for Swift 3

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

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

OUTPUT

"0000000999.1"

MANISH PATHAK
  • 2,602
  • 4
  • 27
  • 31
  • Thanks! Note that default paddingCharacter is "0", but you are correct, setting it safer. – vomi May 31 '20 at 09:14