How to add leading zeros to an Int in Swift
Codemia Knowledge Hub ·
Use String(format: "%04d", number) to turn 42 into "0042", or a NumberFormatter with minimumIntegerDigits set. An Int never stores leading zeros, so padding is always done when you convert the number to a string, for UI output, zero-padded file or image names, or aligned columns.
Why an Int cannot keep leading zeros
Writing the literal with leading zeros does not help:
Swift does not treat a leading zero as an octal prefix (octal literals use 0o), so 0042 is simply the decimal value 42 and prints 42. The zeros are not part of the value, so you need string formatting to show them.
Padding with String(format:)
In %04d:
%starts a format specifier.0pads the number with leading zeros.4is the total width of the output, including the number.dmeans a decimal integer.
Padding with NumberFormatter
For more control, including locale-sensitive formatting, use NumberFormatter:
minimumIntegerDigits sets the minimum number of integer digits; the formatter pads with leading zeros when the number is shorter.
String interpolation cannot pad
Interpolation works for simple conversion but has no direct way to add leading zeros:
Things to keep in mind
- Leading zeros are display only: they never change the value stored in an
Int. - Localization: international apps should consider locale-specific number formats.
- Performance: formatting large datasets adds string-processing overhead, so profile formatting code in performance-sensitive paths.
Quick reference
| Concept | Description | Example |
| Default Int Handling | Swift does not store leading zeros in Int. | let number = 0042 is just 42 |
String(format:) | Formats integers into strings with leading zeros. | String(format: "%04d", 42) |
NumberFormatter | Advanced formatting options, includes locale-aware settings. | numberFormatter.minimumIntegerDigits = 4 |
| No octal from leading zeros | A leading zero does not make a Swift literal octal; octal uses 0o. | 042 is 42, 0o42 is 34 |
| Minimum Integer Digits | Setting the minimum number of digits to ensure leading zeros are added appropriately. | numberFormatter.minimumIntegerDigits |
Related reading
- Leaking views when changing rootViewController inside transitionWithView
- Left-align image and center text on UIButton
- Left Align Cells in UICollectionView
- Lesser than or greater than in Swift switch statement
- Libraries do not get added to APK anymore after upgrade to ADT 22
- Libraries not found when using CocoaPods with iOS logic tests
- library not found for -lPods
- Limit number of characters in uitextview
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.