Swift
Int
Leading Zeros
Programming
Swift Development

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

swift
let number = 42
print(number) // Output: 42

Writing the literal with leading zeros does not help:

swift
let number = 0042
print(number) // Output: 42

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

swift
let number = 42
let formattedNumber = String(format: "%04d", number)
print(formattedNumber) // Output: 0042

In %04d:

  • % starts a format specifier.
  • 0 pads the number with leading zeros.
  • 4 is the total width of the output, including the number.
  • d means a decimal integer.

Padding with NumberFormatter

For more control, including locale-sensitive formatting, use NumberFormatter:

swift
1let number = 42
2let numberFormatter = NumberFormatter()
3numberFormatter.minimumIntegerDigits = 4
4let formattedNumber = numberFormatter.string(from: NSNumber(value: number))
5print(formattedNumber ?? "") // Output: 0042

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:

swift
let number = 42
let formattedNumber = "\(number)"
// There's no direct method to add leading zeros with interpolation

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

ConceptDescriptionExample
Default Int HandlingSwift 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)
NumberFormatterAdvanced formatting options, includes locale-aware settings.numberFormatter.minimumIntegerDigits = 4
No octal from leading zerosA leading zero does not make a Swift literal octal; octal uses 0o.042 is 42, 0o42 is 34
Minimum Integer DigitsSetting the minimum number of digits to ensure leading zeros are added appropriately.numberFormatter.minimumIntegerDigits

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms