NSNumberformatter add extra zero

2 Solutions Collect From Internet About “NSNumberformatter add extra zero”

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];

I think this solves your problem:

[_minutes addObject:[NSNumber numberWithInt:i]];
return [NSString stringWithFormat:@"%02d", [[_minutes objectAtIndex:row] intValue]]; 

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))!