NSNumberformatter add extra zero
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?
- Is there any easy way to round a float with one digit number in objective c?
- Issues with setMaximumFractionDigits
- Swift: NSTextField numberStyle not calling action
- How to put comma and decimals in my UITextField dynamically?
- Struggling with NSNumberFormatter in Swift for currency
- Limit a double to two decimal places without trailing zeros
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];
%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];
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))!
- Delete keychain items when an app is uninstalled
- function with dataTask returning a value
- Swizzling a single instance, not a class
- Is it true Xcode/IB DOES NOT check for protocols?
- In Xcode 4 GM, how do I turn on LLDB? All I see is GDB
- Setting off a function in another class
- Swift – Second section in tableview not visible
- Programatically changing the iOS lock-screen
- UIDatePicker memory leak in IOS 8.3
- Display an image in a scrollview with specific constraints (with auto layout)
- How to hide 'Back' button on navigation bar on iPhone?
- iAd not showing on Device after releasing application on App Store Italy
- Making the launch image display longer xcode
- How to Properly Declare Array of Custom Objects in Swift?
- Xcode 6 won't let me develop on my iOS 8 phone