Make a two-digit string from a single-digit integer
How can I have a two-digit integer in a a string, even if the integer is less than 10?
[NSString stringWithFormat:@"%d", 1] //should be @"01"
3 Solutions Collect From Internet About “Make a two-digit string from a single-digit integer”
I believe that the stringWithFormat specifiers are the standard IEEE printf specifiers. Have you tried
[NSString stringWithFormat:@"%02d", 1];
Use the format string
%02d. This specifies to format an integer with a minimum field-width of 2 characters and to pad the formatted values with 0 to meet that width. See
man fprintf for all the gory details of format specifiers.
If you are formatting numbers for presentation to the user, though, you should really be using
NSNumberFormatter. Different locales have wildly different expectations about how numbers should be formatted.
[NSString stringWithFormat:@"%00.02d", intValue]
This help me to convert 1 to 01.
- How to do screen mirroring using AirPlay from the application (not from control panel) in iOS?
- How can I receive touches using tvOS?
- Keeping an app alive in background unlimited (for a Cydia app)
- With Apps option removed from latest iTunes 12.7… , how to install adhoc IPA?
- How to lock down user to Single App mode in iOS 6, Programmatically?
- App crash when trying to load a large amount off data into my app
- How to disable new Xcode 4.6 warning for whole project ? “… used as the name of the previous parameter rather than as part of the selector”
- Xcode duplicate/delete line
- Make phone call on iPhone and take user back to app? (UIWebView does it)
- Xcode mach-o linker error when archiving project with test targets
- Objective C: Where to remove observer for NSNotification?
- iOS: How to achieve behavior like Android's startActivityForResult
- Xcode variables
- Get image name UIImagePickerController in Swift
- navigation controller bar is being cut off