Leading Zeros in Int in Swift: From Chaos to Order

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

"Missing" Zeros: Why Int Doesn't Store Leading Zeros and How to Live with It?

\n\n

Have you ever tried to display the number 007 on screen and just got 7? A familiar pain? Welcome to the club of Swift developers who sooner or later face the question: "How to add leading zeros to an Int?"

\n\n

This question on Stack Overflow has gathered thousands of views, and it's no surprise. It seems like a simple task — to display an order number as "001234" instead of "1234", or to show time as "09:05" instead of "9:5". But Swift, being a strictly typed language, separates the concept of "number" and "its string representation". Int is a mathematical entity. For it, 007 is simply 7. Leading zeros are lost during initialization.

\n\n

In this article, we will break down all the main approaches to solving this problem. You will learn how to properly format integers, which method to choose for different tasks, and how to avoid a performance pitfall. We will go from simple string masks to the powerful NumberFormatter from Foundation. Let's go!

\n\n

Why Can't Int Store "001"? A Quick Primer

\n\n

Before diving into the code, let's understand the nature of the phenomenon. Int in Swift is a 64-bit (or 32-bit on older devices) signed integer. Its internal representation is binary code. A zero on the left has no mathematical meaning.

\n\n
let number = 007 // Error? No!\nprint(number) // Outputs: 7
\n\n

The Swift compiler won't even let you write 007 in code (unless it's an octal literal, but that uses the 0o prefix). Conclusion: leading zeros are a formatting problem, not a data storage problem. We solve the task at the presentation level (UI, logging, export).

\n\n

Method #1: The Classic — String(format:)

\n\n

The most popular answer on Stack Overflow is using the String(format:) function, inherited from Objective-C (via a bridge with NSString). It's a fast and concise way.

\n\n

Example: Add 3 leading zeros

\n\n
let number = 7\nlet formatted = String(format: "%04d", number)\nprint(formatted) // "0007"
\n\n

The specifier %04d means:

\n
    \n
  • % — start of the specifier;
  • \n
  • 0 — pad empty positions with zeros, not spaces;
  • \n
  • 4 — minimum field width (4 characters);
  • \n
  • d — decimal integer (Int).
  • \n
\n\n

If the number is longer than 4 characters (e.g., 12345), formatting will not truncate it — you will get "12345".

\n\n

Pros and Cons

\n
    \n
  • Pros: Simple, fast, one line. Works in all Swift versions.
  • \n
  • Cons: Uses NSString under the hood, which can be slightly slower than a pure Swift solution. Requires remembering C-style formatting syntax. Not localized (doesn't account for thousand separators if needed).
  • \n
\n\n

When to use: For simple cases: logs, debugging, outputting IDs, when localization is not needed.

\n\n

Method #2: Pure Swift — String(repeating:count:) + Suffix

\n\n

If you want to avoid the Objective-C bridge and write in "pure" Swift, you can do it manually. This is a great exercise for understanding strings.

\n\n
extension Int {\n    func leadingZeros(_ totalDigits: Int) -> String {\n        let stringValue = String(self)\n        let zerosCount = max(0, totalDigits - stringValue.count)\n        return String(repeating: "0", count: zerosCount) + stringValue\n    }\n}\n\nlet number = 42\nprint(number.leadingZeros(5)) // "00042"
\n\n

How does it work?

\n
    \n
  1. Convert the Int to a string.
  2. \n
  3. Calculate how many zeros need to be added: totalDigits - length. If

Blogs

Book Recommendations